I am new to python programming and I am learning python by doing simple programs. Here is what I would like to do: if I have a text file containing numbers:
say this a f1.txt
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 15
fp = open('f1.txt')
lines = fp.readlines()
for ln in lines[0:len(lines)]:
You could do it like this:
a1 =  a2 =  a3 =  a4 =  with open('f1.txt') as f: for line in f: data = line.split() a1.append(int(data)) a2.append(int(data)) a3.append(int(data)) a4.append(int(data))
So first of all, we use the
with statement to open the file. This makes sure that the file is automatically closed even when errors appear. It’s just nicer that way. While the file is open
f will be the file handle.
Now, Python allows us to iterate over the lines of a file simply by iterating over the file handle. So
for line in f will iterate over all lines automatically. There is no need to call
readlines() first, and certainly no need to do
lines[0:len(lines)] which essentially only creates a copy of the list—you could just iterate over
Now inside of the loop, we take the line, and split it by whitespace—without arguments
str.split will always do that.
str.split returns a list, so we store that in an extra variable. Next we append each column to the correct list. And as you want the values as numbers, we convert them to integers.
str.strip you mentioned basically takes off any leading or trailing whitespace of the string. As we are using
str.split without arguments, extra whitespace will be removed too, so we don’t really need that.
Finally, having four separate lists stored in separate variables is a bit annoying to maintain. You could simply create a list of lists instead:
a = [, , , ] # A list with four empty lists
And then, inside of the loop, you can just append
for i, value in enumerate(line.split()): a[i].append(int(value))
When iterating over
enumerate, you will not only get the value (which you would get when iterating just over the list), but also the index. So using this, we get the index of each element within the splitted line and can automatically append it to the correct sublist of