5.9. Files¶
5.9.1. Writing variables to a text file¶
Create a program (use f_write1.py
for the filename) that writes:
first row
second row
to a new file.
Question What would be a good last statement in your program?
outFile.close
outFile.write("second row")
outFile.close()
write(outFile, "second row")
In a previous section, you made the program man_tab4.py
. If you have lost it, it is below. Run it again to see what it did.
n = 0.01 def manning(slope, hydraulic_radius,n): # flow speed v = (((hydraulic_radius)**(2.0/3.0))*(slope**(1.0/2.0)))/n return v print( "mannings n is:", n, "(-)\n") print("slope (-)\thydr. radius (m)\tvelocity (m/s)") x = 0.1 while x < 0.16: x = x + 0.01 y = 2.0 while y < 4.1: y = y + 1 velocity = manning(x,y, n) print(x, "\t\t", y, "\t\t\t", velocity)
As you see, it prints a lot to screen. Now, let’s try to save this in a file instead. Let’s start with just the first line. Save man_tab4.py
as f_write2.py
. Add statements closing and opening a file, and modify the line:
print("mannings n is: {} (-)\n".format(n))
in such a way that it writes to the file instead of printing to the screen. If you don’t manage, answering the question below might help you.
Question You can only write a variable of type string to a file. How could you convert a variable to a string?
string(n)
str(n)
n.string()
Open f_write2.py
and save it as f_write3.py
. Modify f_write3.py
until it writes all output of man_tab4.py
to a file, formatted in the same way!
Question Where did you type the statement(s) that write each line of the table to the file?
Inside the main loop (below the first
while
statement).Inside the second loop nested inside the main loop (below the second
while
statement).Below the two loops (outside the loops).
5.9.2. Reading from a text file¶
Reading data from a file can be done in many different ways. Here you will use the read and readlines methods. The ascii file data.col
in the table below contains observations of the pH in the soil. The first and second columns contain the x and y coordinates of the observations, respectively. The third column contains the pH values. A filename with sufix .col
was used here, since the data are formatted in columns, but any other name would be possible. This is quite a small file, that could be edited manually, but in many cases, data files are much larger, and a program is needed for modifying the contents or the format of the file. Here, you will write such a program.
12.3 134.2 -9 32.5 124.5 4.5 25.6 145.2 8.9 90.3 131.2 7.3 19.0 130.4 6.4 0.0 090.2 5.4 12 080.1 8.1 14.5 190.4 6.9 3.5 137.9 6.4 4.9 112.4 8.0 13.5 123.2 7.5 24.5 112.5 7.1 343.2 234.1 7.4 0.1 142.1 7.3 11.3 134.1 3.4 31.5 114.5 4.5 15.6 145.1 -9 90.3 131.1 7.3 19.0 130.4 -9 0.0 090.1 5.4 11 080.1 8.1 14.5 190.4 6.9 3.5 137.9 6.4 4.9 111.4 8.0 13.5 113.1 7.5 24.5 111.5 7.1 343.1 134.1 7.4 0.1 141.1 -9
Normally, you would have the file on your harddisk. Here, you need to create it first. Copy-paste it to your editor (also used for writing programs) and save it as data.col
. Now, write a program that reads the contents of the file and prints it to screen. Use the read
method. Save it as f_read1.py
.
Question What is the data type of the variable that is returned by the read
method?
Table
Floating point
Integer
String
Now, let’s look at the readlines
method. Create the program f_read2.py
given in the table below. Execute it.
inFile = open("data.col","r") a = inFile.readlines() print(a) inFile.close()
Question What is the type of a
and a[0]
, respectively?
A list and a string.
A string and a list.
Both lists.
Both strings
Create a program f_read3.py
using the readlines
method that prints separately each line of the file data.col
to screen. You need to loop over the ‘lines’.
Question What statement can be used here to create the loop?
while
is possible, butfor
is notboth
while
andfor
is possibleif
is the only statement that can be used herefor
is possible, butwhile
is not
Now, assume another existing program that you just bought needs to read the contents of data.col
, but:
the other program cannot read missing values (indicated by
-9
indata.col
)the other program can only import the file when columns are separated by a comma (,`) instead of whitespace characters (as in
data.col
).
Open f_read3.py
and save it as f_read4.py
. Modify it, such that it does not print the lines containing a missing value for the pH.
Question Which method can be used here?
str.split
str.mv
str.capitalize
str.find
Next, we need to replace the whitespace inbetween the columns with comma’s, such that the output looks like this:
32.5,124.5,4.5 25.6,145.2,8.9 90.3,131.2,7.3 19.0,130.4,6.4 0.0,090.2,5.4 12,080.1,8.1 14.5,190.4,6.9 3.5,137.9,6.4 4.9,112.4,8.0 13.5,123.2,7.5 24.5,112.5,7.1 343.2,234.1,7.4 0.1,142.1,7.3 11.3,134.1,3.4 31.5,114.5,4.5 90.3,131.1,7.3 0.0,090.1,5.4 11,080.1,8.1 14.5,190.4,6.9 3.5,137.9,6.4 4.9,111.4,8.0 13.5,113.1,7.5 24.5,111.5,7.1 343.1,134.1,7.4
Open f_read4.py
and save as f_read5.py
. Modify it, printing comma’s instead of whitespace between columns. You only need to change the print statement! As follows:
inFile = open("data.col","r") a = inFile.readlines() for aLine in a: # each line splitted in three separate strings stored as list items aLineList = str.split(aLine) # the third list item, i.e. pH pH = aLineList[2] # print only if pH is not a missing value if not(pH == "-9"): print(aLineList[0] + "," + aLineList[1] + "," + aLineList[2]) inFile.close()