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

前端 未结 3 891
执笔经年
执笔经年 2021-01-04 19:14

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?

相关标签:
3条回答
  • 2021-01-04 19:29

    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.

    0 讨论(0)
  • 2021-01-04 19:41

    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.

    0 讨论(0)
  • 2021-01-04 19:42

    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.

    0 讨论(0)
提交回复
热议问题