简体   繁体   中英

ValueError: too many values to unpack 4

for n, row in spamreader:
    print('row')
    print('debug point 4')
    cur.execute('''INSERT INTO table(col1,col2,col3,col4)values (%s,%s,%s,%s)''',row)
    if n % 2 == 0:
        connect.commit()
 connect.commit()

The file that I am reading from has exactly 4 columns. Still I am getting the below error

File "test_new.py", line 22, in <module>
    for n, row in spamreader:
ValueError: too many values to unpack

try

 for n, row in spamreader.iterrows():

for pandas dataframe you have to use df.iterrows():

n will be your index. in row you can access your data like row['column_name']

The problem is that when you iterate over spamreader you expect two variables - n - which according to the rest of the code is probably the line number row - the row itself.

What you actually need to do in order to get the row number is -

for n, row in enumerate(spamreader):

Assuming spamreader reads the rows as expected (definition of spamreader is not given currently) this should do the job.

See more about using enumerate here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM