In some of the first-year problems you are required to read in
numerical date from a file. If each line contains only one element
then this is generally simple--the details are discussed in Section
3.12. However, if the file you are reading in from is
itself a table, i.e. each line contains more than one datum, you will
need to use the string library.
Begin your program with from string import * to have access to
the functions you will need in this section. Then create a file object
and read in the first line as usual. fin is the conventional
name for a file variable that points to an input file, although you
are free to call a file variable anything you would call any other
variable. The data read in is just an example.
>>> fin = open("input.dat", "r")
>>> line = fin.readline()
>>> print line
1 5.06 78 15
The line variable is then simply a string containing the
contents of the line. Assuming the individual datum on the line
are separated by white space, i.e. any number of spaces or tabs,
you now want to split the string into individual numbers; in this case
1, 5.06, 78, and 15. This is done using the split() function.
The split function takes at least one argument: the string which it
should split into a list:
>>> data = split(line)
>>> print data
['1', '5.06', '78', '15']
The data variable is now a list, each element of which contains
each nubmer on the line. Unfortunately, each element is still in the
form of a string so you will generally need to coerce them into
numbers using either int() or float(). Recall lists can
be referenced by offset in exactly the same way as arrays:
>>> x = int(data[0])
>>> print x
1
>>> y = float(data[1])
>>> print y
5.06
If you would like to read in the lines of a file until you reach then
either use the readlines() method (note the plural) which will
return an ordered list where each element is a string containing the
contents of a line, or use the following code:
line = fin.readline()
while line != '':
# Manipulate and use the data from the current line and then...
line = f.readline()
This will repeat the contents of the while loop until the end
of the file is reached, i.e. until line is an empty string:
''.