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: sureronald is an unknown quantity at this point 
Solved Threads: 16
sureronald sureronald is offline Offline
Junior Poster in Training

Read large input file to memory

 
0
  #1
Oct 10th, 2009
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
  1. data=fPtr.readlines()
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
Catch me here!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,069
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Oct 10th, 2009
So the question is:
How large is your data file?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,037
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 293
woooee woooee is offline Offline
Veteran Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: ramadhani victo is an unknown quantity at this point 
Solved Threads: 1
ramadhani victo ramadhani victo is offline Offline
Newbie Poster
 
0
  #4
Oct 11th, 2009
aq mau minta gambar unit input,tolong di kirim ke mbapong_strees@yahoo.co.id
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 97
Reputation: sureronald is an unknown quantity at this point 
Solved Threads: 16
sureronald sureronald is offline Offline
Junior Poster in Training
 
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
  1. li=[]
  2. while True:
  3. li.append("king")
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!
Catch me here!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
Oct 12th, 2009
As performance is concerned, the file read from disk will be the slowest part by far!
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC