9

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

f1.txt:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 15


fp = open('f1.txt')
a1=[]
a2=[]
a3=[]
a4=[]
lines = fp.readlines()

for ln in lines[0:len(lines)]:
line=ln.strip().split()
a1=line();

fp.close()

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?

2
  • Please use correct indentation as this won't run. Commented Jan 22, 2014 at 14:33
  • This is going to get better responses if you narrow the question to one thing. You might try running your code and asking specifically about any error messages you get, or why the output differs from your expected output. Commented Jan 22, 2014 at 14:34

4 Answers 4

18

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[0]))
        a2.append(int(data[1]))
        a3.append(int(data[2]))
        a4.append(int(data[3]))

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()):
    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 a.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, the four append lines are not good. I'd show him how to use a loop for that. Plus, he wants numbers, so let's mention int().
3
data = []
for line in lines:
    data.append([int(v) for v in line.split()])

or

data = [[int(v) for v in line.split()] for line in lines]

EDIT: To answer the comment - code below will rearrange the data as required list of numbers

numbers = zip(*data)

2 Comments

This will make a list of rows, instead of a list of columns.
@poke, mission accomplished :)
1

line[0], line[1], etc. should give you the first, second, etc. entry in each line.

The split() function will split the given line at whitespace and returns a list of the entries.

Comments

0

Your indentation is wrong in the for loop. All the code that you want included in the loop should be indented 4 spaces.

The line a1= line() won't do anything. The syntax a = A() would set a equal to the result of a function A() or to a new instance of a class A. If you want to add line to the list a1 you need to use a1.append(line)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.