1

This is a derivative from this question. In that one, the following line comes up in a pythonscript:

c1,c2 = [float(x) for x in line.split()] #convert line into 2 floats and unpack

when reading a data file with two columns.

Now, my real data file has 31 columns and I need to use columns 28 and 31 instead of 1 and 2. When I try the simplest (and ugliest) approach at escalating that line:

c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,c30,c31 = [float(x) for x in line.split()]

I get the error:

ValueError: too many values to unpack

How can I properly read all the columns in my file?

1
  • Do all rows have 31 columns? If the rows are "jagged" [ with some having less than or more than 31 columns ], this approach will fail. Commented Oct 23, 2012 at 13:40

2 Answers 2

2

Another approach:

cols = [float(x) for x in line.split()]
c28, c31 = cols[27], cols[30]

So, the overall setup will look something like:

with open('<file name here>') as source_file:
    for line in source_file:
        cols = [float(x) for x in line.split()]
        c28, c31 = cols[27], cols[30]
Sign up to request clarification or add additional context in comments.

1 Comment

Great, I'm picking this one mainly because it makes clear that the array starts at 0 which is not a trivial matter (not knowing this was giving me strange results) Thank you!
1
cols = [float(x) for x in line.split()]
for i in len(cols):
    print i, cols[i]

there's no need to instantiate N variables, just use the array you have while reading the file

the for loops in this case helps you see how many values you really have

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.