-2

I have a file that I want to unpack and utilise the columns in different files. The issue I have is that the file I want to unpack varies from row to row on the number of columns it has (for example row 1 could have 7 columns, row 2 could have 15).

How do I unpack the file without receiving the error "Too many values to unpack"?

filehandle3 = open ('output_steps.txt', 'r')
filehandle4 = open ('head_cluster.txt', 'w')
for line in iter(filehandle3):
    id, category = line.strip('\n').split('\t')
    filehandle4.write(id + "\t" + category + "\n")
filehandle3.close()
filehandle4.close()

Any help would be great. Thanks!

user2320229
  • 71
  • 1
  • 7
  • use *, and ** functionality in python.. [refer this](http://stackoverflow.com/questions/3394835/args-and-kwargs) – BomberMan Dec 01 '14 at 10:53

1 Answers1

1

You should extract the values separately, if present, e.g. like this:

for line in iter(filehandle3):
    values = line.strip('\n').split('\t')
    id       = values[0] if len(values) > 0 else None
    category = values[1] if len(values) > 1 else None
    ...

You could also create a helper function for this:

def safe_get(values, index, default=None):
    return values[index] if len(values) > index else default

or using try/except:

def safe_get(values, index, default=None):
    try:
        return values[index]
    except IndexError:
        return default

and use it like this:

    category = safe_get(values, 1)

With Python 3, and if the rows always have at least as many elements as you need, you can use

for line in iter(filehandle3):
    id, category, *junk = line.strip('\n').split('\t')

This will bind the first element to id, the second to category, and the rest to junk.

tobias_k
  • 81,265
  • 12
  • 120
  • 179