user3215074 user3215074 - 1 year ago 160
Python Question

How to read numbers in text file using python?

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)]:


I want to get first column in a1, second in a2 and so on. I know above code may be wrong, please tell me where I went wrong and how to correct it. Especially I am not understanding command 'ln.strip().split()'. Can someone help?

Answer Source

You could do it like this:

a1 = []
a2 = []
a3 = []
a4 = []

with open('f1.txt') as f:
    for line in f:
        data = line.split()

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 lines too.

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.

The 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 data[i] to a[i]:

for i, value in enumerate(line.split()):

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 a.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download