使用 izip 在 python 中同时读取两个文件中的行
我正在尝试使用“izip”同时从两个文件中读取行,但是出现如下错误:
>>> f1=open('/home/xug/scratch/test/test_1.fastq','r')
>>> f2=open('/home/xug/scratch/test/test_2.fastq','r')
>>> from itertools import izip
>>> for i,line1,line2 in izip(f1,f2):
... if i%4==3:
... print line1,line2
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
我不知道为什么......什么是“需要超过 2 个值”? 谢谢
I'm trying to use 'izip' to read lines simultaneously from two files, however got errors like below:
>>> f1=open('/home/xug/scratch/test/test_1.fastq','r')
>>> f2=open('/home/xug/scratch/test/test_2.fastq','r')
>>> from itertools import izip
>>> for i,line1,line2 in izip(f1,f2):
... if i%4==3:
... print line1,line2
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
I don't know why....what is "need more than 2 values"?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
izip() 只是将两个可迭代对象压缩成对。它没有像您所期望的那样引入额外的计数器。尝试使用 enumerate()
来获取该计数器。
该错误消息是由于尝试将可迭代的第一项分配给
i, line1, line2
导致的。由于第一项是一对字符串,例如s1
和s2
,因此此分配本质上相当于显然需要两个以上的值来解包。 (准确地说,需要三个。)
izip()
simply zips the two iterables to pairs. It doesn't introduce an additional counter, as you seem to expect. Try usingenumerate()
to also get that counter.
The error message results from the attempt to assign the first item of your iterable to
i, line1, line2
. Since the first item is a pair of strings, says1
ands2
, this assignment would be essentially equivalent towhich would clearly needs more than two values to unpack. (To be precise, it would need three.)