0

Inspired by the accepted answer to Reading a UTF8 CSV file with Python, I've tried the following but get

ValueError: need more than 5 values to unpack

Seems the empty strings are tripping me here. Is there a function I should apply to row to make this work? I'll need to work with these variables individually further down the line. Thanks.

 def encodevar(x):
     return x

 row = ['85123', '', '123 The Address', '', '', 'TUCSON', 'AZ ', 'The Company Name', '', '']
 for zip5,zip4,strname,strnumber,strsuite,city,state,busname,lname,fname in row:
            HHid = encodevar( zip5 )     +\
                   encodevar( zip4 )     +\
                   encodevar( stname )   +\
                   encodevar( stnumber ) +\
                   encodevar( stsuite )  +\
                   encodevar( busname )
Community
  • 1
  • 1
user2105469
  • 1,413
  • 3
  • 20
  • 37

1 Answers1

3

You are looping over row, which means that each element is iterated over separately.

Just assign directly to your variables:

zip5, zip4, strname, strnumber, strsuite, city, state, busname, lname, fname = row

Because you were looping over the individual values, you were trying to unpack zip5 value into your 10 names:

zip5, zip4, strname, strnumber, strsuite, city, state, busname, lname, fname = '85123'

Python then treats that string value, '85123' as a sequence of 5 characters, finding that those 5 don't fit into 10 names, throwing the exception you saw.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343