ValueError:需要多于1个值才能解压缩

时间:2011-07-14 02:19:26

标签: python string

我有一个代码用于在大型文本文件中修改字符串。当我运行它时,错误消息显示“ValueError:需要多于1个值才能解压缩”。所以我在这一行中更改了0,1和2的整数:

    _, k = line.split('\t',1)

如果我输入2,则表示“打开包装的价值太多”。我尝试将每个变量定义如下:

    f = []
    _ = []
    k = []
    x = []
    i = 0

..那也没有成功。

以下是代码:

    # Opens each file to read/modify
    infile='110331_HS1A_1_rtTA.result'
    outfile='2.txt'

    #import Regex
    import re

    with open (infile, mode='r', buffering=-1) as in_f, open (outfile, mode='w', buffering=-1) as out_f:
        f = (i for i in in_f if i.rstrip())
        for line in f:
            _, k = line.split('\t',1)
            x = re.findall(r'^1..100\t([+-])chr(\d+):(\d+)\.\.(\d+).+$',k)
            if not x:
                continue
            out_f.write(' '.join(x[0]) + '\n')

这是错误:

   Traceback (most recent call last):
     File "C:\DB\Stack Overflow\test5.py", line 11, in <module>
       _, k = line.split('\t',1)
   ValueError: need more than 1 value to unpack

感谢那些愿意帮助/教我的人:)

2 个答案:

答案 0 :(得分:2)

您点击的行不包含\t,因此结果列表只有一个元素。

答案 1 :(得分:1)

哦,那是我在other question上写的代码。你本可以问那里......

由于您提供的文件没有tab的任何非空行,我认为没有问题。

您可以通过更改此行来检查行中是否有任何制表符:

f = (i for i in in_f if i.rstrip())

f = (i for i in in_f if '\t' in i.rstrip())

因此,您将删除空行以及没有'\t'的行。 要查看具有该问题的文件行,您可以(使用原始代码):

try:
    _, k = line.split('\t',1)
except:
    print(line)

顺便说一下,_经常用于我们在某些操作中不关心的事情。它可以有任何名称。

ATCGATTTCG....ATGC    1..100blablabla
       ^                     ^
 useless part (_)     important one (k)