Python ValueError:没有足够的值来解包(预期 11,得到 1)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34627211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:17:02  来源:igfitidea点击:

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

pythoncsv

提问by Mr.Sharp

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']

采纳答案by Martin Konecny

For the line

对于线

line.split()

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

你在分裂什么?看起来像一个 CSV,所以试试

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.

正如@TigerhawkT3 所提到的,最好使用 CSV 模块。此处提供令人难以置信的快速简便的方法。

回答by xbonez

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.

期望line.split()产生 5 个元素,但在您的情况下,它只产生 1 个元素。这可能是因为数据不是您期望的格式、格式错误的流氓行,或者可能是空行 - 无法知道。

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.

正如马丁所建议的那样,您也可能在错误的分隔符上进行拆分。

回答by Rohith Yeravothula

Looks like something is wrong with your data, it isn't in the format you are expecting. It could be a new line character or a blank space in the data that is tinkering with your code.

看起来您的数据有问题,它不是您期望的格式。它可能是修改代码的换行符或数据中的空格。