Reading from input file.
Mike Meyer
mwm at mired.org
Thu Jan 12 10:05:00 EST 2006
"Sheldon" <shejo284 at gmail.com> writes:
> after you have read the file then split it like this:
> file = open('inputfile.txt', 'r').read()
> import string
> file = string.split(file,'\n')
You're doing things the hard way - at least if you have a modern
Python. The above can be done as:
file = open('inputfile.txt', 'r').read().split('\n')
Or even better
file = open('inputfile.txt', 'r').readlines()
will all give you the same result.
BTW, it's a bad habit to leave open files laying around. It's also a
bad habit to use the name of builtins (like "file") as variable names.
> now if you print file[0] you should only get the first line.
> Be careful and examine the file for non-continuous sections where a
> line is empty. That is to say where file[x] = ' '. You should remove
> these lines from the txt files before you read it with python. It maybe
> so that python reads the entire file, empty spaces and all, and then
> you have to remove them after the split with a WHERE statement. This is
> a much easier way so lets hope that this is so.
Huh? What in the OP makes you think this is even necesary?
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list