我有一个像输入一样的大文件,每个4行对应于以@开头的行。第二行(在@之后)是一系列字符,对于某些ID,我们没有这一行。如果是这种情况,我想删除所有属于同一个ID的4行。
我也试过下面的代码在Python中,并给出了错误。编辑文本文件时出错
输入:
@M00872:361:000000000-D2GK2:1:1101:16003:1351 1:N:0:1
ATCCGGCTCGGAGGA
+
1AA?ADDDADDAGGG
@M00872:361:000000000-D2GK2:1:1101:15326:1352 1:N:0:1
GCGCAGCGGAAGCGTGCTGGG
+
CCCCBCDCCCCCGGEGGGGGG
@M00872:361:000000000-D2GK2:1:1101:16217:1352 1:N:0:1
+
输出:
@M00872:361:000000000-D2GK2:1:1101:16003:1351 1:N:0:1
ATCCGGCTCGGAGGA
+
1AA?ADDDADDAGGG
@M00872:361:000000000-D2GK2:1:1101:15326:1352 1:N:0:1
GCGCAGCGGAAGCGTGCTGGG
+
CCCCBCDCCCCCGGEGGGGGG
import fileinput
with fileinput.input(files="4415_pool.fastq", inplace=True, backup="file.bak") as f:
for l in f:
if l.strip().startswith("@"):
c = 2
next_line = f.readline().strip()
if not next_line:
while c:
c -= 1
try:
next(f)
except StopIteration:
break
else:
print(l.strip())
print(next_line.strip())
while c:
c -= 1
try:
print(next(f).strip())
except StopIteration:
break
,但没有工作,给了这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: FileInput instance has no attribute '__exit__'
你知道如何解决这个问题?
你正在使用哪个python版本?我认为这是旧版本不支持fileinput与。因此,使用'f = fileinput.input(files =“4415_pool_TCP_Ctrl.fastq”,inplace = True,backup =“file.bak”) –
python的版本是:2.7 – ARM