Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: not enough values to unpack (expected 11, got 1)

Tags:

python

csv

I wrote a script for system automation, but I'm getting the error described in the title. My code below is the relevant portion of the script. What is the problem?

import csv
import os

DIR = "C:/Users/Administrator/Desktop/key_list.csv"

def Customer_List(csv):
    customer = open(DIR)
        for line in customer:
            row = []
            (row['MEM_ID'],
             row['MEM_SQ'],
             row['X_AUTH_USER'],
             row['X_AUTH_KEY'],
             row['X_STORAGE_URL'],
             row['ACCESSKEY'],
             row['ACCESSKEYID'],
             row['ACCESSKEY1'],
             row['ACCESSKEYID1'],
             row['ACCESSKEY2'],
             row['ACCESSKEYID2'])=line.split()
            if csv == row['MEM_ID']:
                customer.close()
                return(row)
            else:
                print ("Not search for ID")
                return([])

id_input = input("Please input the Customer ID(Email): ")
result = Customer_List(id_input)

if result:
    print ("iD:    " + id['MEM_ID']
like image 321
Mr.Sharp Avatar asked Jan 06 '16 06:01

Mr.Sharp


People also ask

How do I fix ValueError is not enough values to unpack in python?

The “ValueError: not enough values to unpack” error is raised when you try to unpack more values from an iterable object than those that exist. To fix this error, make sure the number of values you unpack from an iterable is equal to the number of values in that iterable.

How do I fix not enough values to unpack?

Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same. This will resolve the value error.

How do I fix too many values to unpack in python?

We get this error when there's a mismatch between the number of variables to the amount of values Python receives from a function, list, or other collection. The most straightforward way of avoiding this error is to consider how many values you need to unpack and then have the correct number of available variables.


2 Answers

For the line

line.split()

What are you splitting on? Looks like a CSV, so try

line.split(',')

Example:

"one,two,three".split()  # returns one element ["one,two,three"]
"one,two,three".split(',')  # returns three elements ["one", "two", "three"]

As @TigerhawkT3 mentions, it would be better to use the CSV module. Incredibly quick and easy method available here.

like image 113
14 revs, 12 users 16% Avatar answered Oct 10 '22 07:10

14 revs, 12 users 16%


The error message is fairly self-explanatory

(a,b,c,d,e) = line.split()

expects line.split() to yield 5 elements, but in your case, it is only yielding 1 element. This could be because the data is not in the format you expect, a rogue malformed line, or maybe an empty line - there's no way to know.

To see what line is causing the issue, you could add some debug statements like this:

if len(line.split()) != 11:
    print line

As Martin suggests, you might also be splitting on the wrong delimiter.

like image 27
xbonez Avatar answered Oct 10 '22 09:10

xbonez