简体   繁体   中英

ValueError: too many values to unpack (expected 2) in python3

I checked similar questions in this error but none is useful here is my code:

def update(up_margin=None, margin=None, time=None, history=None, clean_type=None):
    update_column = ''

    columns = {'up_margin': up_margin, 'margin': margin, 'time': time, 'history': history, 'type': clean_type}
    for key, value in columns:
        if value is not None:
            if update_column != '':
                update_column += ','
            update_column += '{}={}'.format(key, value)
    print(update_column)

update(up_margin=100) 

You need to iterate over the items() of your dictionary.

Change your for loop to

for key, value in columns.items():
def update(up_margin=None, margin=None, time=None, history=None, clean_type=None):
    update_column = ''

    columns = {'up_margin': up_margin, 'margin': margin, 'time': time, 'history': history, 'type': clean_type}
    for key, value in columns.items():
        if value is not None:
            if update_column != '':
                update_column += ','
            update_column += '{}={}'.format(key, value)
    print(update_column)

update(up_margin=100) 

When iterating over a map, use.items()

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