Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编辑文本文件时出错_Python - Fatal编程技术网

Python 编辑文本文件时出错

Python 编辑文本文件时出错,python,Python,我有一个像输入一样的大文件,每4行对应相同的ID,即以@开头的行。@之后的第二行是字符序列,对于某些ID,我们没有这一行。如果是这种情况,我想删除所有4行属于相同的ID。 我还用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

我有一个像输入一样的大文件,每4行对应相同的ID,即以@开头的行。@之后的第二行是字符序列,对于某些ID,我们没有这一行。如果是这种情况,我想删除所有4行属于相同的ID。 我还用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__'

您知道如何解决此问题吗?

似乎fileinput.fileinput类没有实现在与fileinput.input一起使用时所需的“退出”方法。。语句。

似乎fileinput.fileinput类没有实现在与fileinput.input一起使用时所需的_退出_;方法。。语句。

我认为问题在于python版本2.7不支持使用

使用

反而

with fileinput.input(files="4415_pool.fastq", inplace=True, backup="file.bak") as f

我认为问题在于python版本2.7,它不支持使用

使用

反而

with fileinput.input(files="4415_pool.fastq", inplace=True, backup="file.bak") as f

虽然with语句是在2.5中添加的,但我不认为fileinput被移植为使用它contextlib

您的代码将在python3中工作,但在2.7中不工作。要解决此问题,请使用py3或移植您的代码在以下行上进行迭代:

   with open(filename, "r") as f:
         lines = f.readlines()

   for line in lines: 
        #do whatever you need to do for each line. 

虽然with语句是在2.5中添加的,但我不认为fileinput被移植为使用它contextlib

您的代码将在python3中工作,但在2.7中不工作。要解决此问题,请使用py3或移植您的代码在以下行上进行迭代:

   with open(filename, "r") as f:
         lines = f.readlines()

   for line in lines: 
        #do whatever you need to do for each line. 

为了解决您在2.7中提出的问题,我将执行以下操作:

# Read all the lines in a buffer
with open('input.fastq', 'r') as source:
  source_buff = iter(source.readlines())

with open('output.fastq', 'w') as out_file:
  for line in source_buff:
    if line.strip().startswith('@'):
      prev_line = line
      line = next(source_buff)

      if line.strip():
        # if the 2nd line is not empty write the whole block in the output file
        out_file.write(prev_line)
        out_file.write(line)
        out_file.write(next(source_buff))
        out_file.write(next(source_buff))
      else:
        pass

我知道。fastq文件有时可能非常大,因此我建议将此代码放入一个循环中,每次读取4行或尽可能多的块行,而不是读取缓冲区中的整个文件。

作为2.7中问题的解决方案,我将执行以下操作:

# Read all the lines in a buffer
with open('input.fastq', 'r') as source:
  source_buff = iter(source.readlines())

with open('output.fastq', 'w') as out_file:
  for line in source_buff:
    if line.strip().startswith('@'):
      prev_line = line
      line = next(source_buff)

      if line.strip():
        # if the 2nd line is not empty write the whole block in the output file
        out_file.write(prev_line)
        out_file.write(line)
        out_file.write(next(source_buff))
        out_file.write(next(source_buff))
      else:
        pass

我知道。fastq文件有时可能非常大,因此我建议不要在缓冲区中读取整个文件,而是将此代码放在一个循环中,每次读取4行或与块一样多的行。

您使用的是哪种python版本?我认为它是旧版本,不支持使用fileinput。因此,请使用f=fileinput.inputfiles=4415\u pool\u TCP\u Ctrl.fastq,inplace=True,backup=file.bak python的版本是:2.7您正在使用哪个python版本?我认为它是旧版本,不支持使用fileinput。所以使用f=fileinput.inputfiles=4415\u pool\u TCP\u Ctrl.fastq,inplace=True,backup=file.bak python的版本是:2.7