| | |
Read large input file to memory
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2008
Posts: 97
Reputation:
Solved Threads: 16
I wrote a python program that gets input from an input file. It works fine for small input files since after opening the file I did this to get the data
Since readlines only takes the input data and packs it into a list, it is clear it won't work for large input files. The problem is that I need to extract all the data in the input file before I begin any operation.
I will be doing lots of looping in the program and I don't know whether opening and closing the file in a single loop would be efficient.
Please advise on the best option.
Happy times
python Syntax (Toggle Plain Text)
data=fPtr.readlines()
I will be doing lots of looping in the program and I don't know whether opening and closing the file in a single loop would be efficient.
Please advise on the best option.
Happy times
Catch me here!
•
•
Join Date: Dec 2006
Posts: 1,028
Reputation:
Solved Threads: 289
0
#3 Oct 10th, 2009
A Python list will hold something like 2 trillion items, but is going to be pretty slow with a very large number or records in it. If your list is going to be 100 million records or more, then consider an SQLite database instead. If it's a paltry one million records (we now live in a gigabyte world), then there should not be a problem, but you might want to consider using a dictionary or set as they are both indexed via a hash and would be much faster on lookups.
Linux counter #99383
•
•
Join Date: May 2008
Posts: 97
Reputation:
Solved Threads: 16
0
#5 Oct 12th, 2009
I realized that a python list can hold as much data as the computer memory allows. On the python interpreter I gave this lines just to verify this and then on one console I gave the top command just to monitor the memory consumption of the python interpreter
There was no error, the size of the list increased infinitely and hence the memory consumption of the python interpreter.
The reason I posted this question is that I thought it was a bug in a program I had submitted to some online judge who normally test a program with large input files.
Many thanks to all contributors!
python Syntax (Toggle Plain Text)
li=[] while True: li.append("king")
The reason I posted this question is that I thought it was a bug in a program I had submitted to some online judge who normally test a program with large input files.
Many thanks to all contributors!
Catch me here!
![]() |
Similar Threads
- How to read a very large binary files? (C++)
- read text file into memory and loop through lines (C#)
- Replace Line in .txt File (C++)
- Can't Read Input File (C++)
- Adding values read from input file? (C)
- Need help using a string input file??? (C++)
- Big Text File Processing (Python)
- How to count/keep track of letters from file input in C? (C)
- Reading an input file as a class memeber function (C++)
- How to Read an input file (C++)
Other Threads in the Python Forum
- Previous Thread: BaseHTTPServer
- Next Thread: constant updates
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlwt






