473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

reading in lines from a file -FAST!

Hi
I have a file containing 168092 lines (each line a single word) and when
I use

for line in f:
s = s + line

it takes for ages to read it all in - so long in fact that it makes the
program unusable. Is there any way to do something like C's fread in
Python so that I can just slurp in 1.7MB of data at one go, rather than
reading line by line?

Thanks,
Rajarshi
Jul 18 '05 #1
3 5187
Rajarshi Guha wrote:
Hi
I have a file containing 168092 lines (each line a single word) and when
I use

for line in f:
s = s + line

it takes for ages to read it all in - so long in fact that it makes the
program unusable. Is there any way to do something like C's fread in
Python so that I can just slurp in 1.7MB of data at one go, rather than
reading line by line?

Thanks,
Rajarshi


Assuming f is an open File Object, you can use:

string = f.read()

Which reads the entire content of f into string. Notice, though, that
this solution isn't scalable: reading an entire file to memory becomes
messier the larger the file, whereas reading it one line at a time works
pretty much the same no matter how big the file is.

Adam

Jul 18 '05 #2
Why not use

s=openfile.read()
s=string.split(s,'\n')

just a thought
sree

Jul 18 '05 #3
In article <pa**********************************@presidency.c om>,
Rajarshi Guha <ra******@presidency.com> wrote:
Hi
I have a file containing 168092 lines (each line a single word) and when
I use

for line in f:
s = s + line

it takes for ages to read it all in - so long in fact that it makes the
program unusable. Is there any way to do something like C's fread in
Python so that I can just slurp in 1.7MB of data at one go, rather than
reading line by line?


How about:
s = f.read()
(assuming f is a file object)

-Mark
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Boris Boutillier | last post by:
HI all, I came across a strange behaviour with read method, and I'm not sure if this is a filesystem problem or a misuse of this method. I do the simple following thing : f = open...
14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
3
by: Steven Burn | last post by:
The application; Service on my webserver that allows a user to upload their HOSTS file for functions to verify the contents are still valid. Uses; 1. XMLHTTP (MSXML2) 2. FileSystemObject...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.