【问题标题】:What does this error mean? Python error [duplicate]这个错误是什么意思? Python错误[重复]
【发布时间】:2014-10-03 23:20:23
【问题描述】:
line = line.strip()
rsid, chromosome, position, genotype = line.split(",")

它给了我一个价值错误说

ValueError: Need more than one value to unpack

我该如何解决这个问题?

【问题讨论】:

  • 这意味着你的行很可能是empty
  • 这意味着 line.split() 只返回一个值。所以它可能是空的
  • print(line.spit(',')) 会告诉你问题。

标签: python


【解决方案1】:

错误告诉您line.split(',') 返回的列表中只有 一个 字符串。这意味着该字符串中没有逗号,并且拆分返回的字符串保持不变,但在一个列表中。

通常,这意味着您的字符串开头为空:

>>> 'string with no commas'.split(',')
['string with no commas']
>>> ''.split(',')
['']

您可以轻松跳过空行:

line = line.strip()
if not line:
    continue  # next iteration of the loop
rsid, chromosome, position, genotype = line.split(",")

您可能想查看csv module 而不是自己拆分文件行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2016-03-24
    • 2015-10-27
    相关资源
    最近更新 更多