读取 gzip 文本文件 line-by-line 以在 python 3.2.6 中处理

Reading gzipped text file line-by-line for processing in python 3.2.6

在 python 方面,我完全是个新手,但我的任务是尝试在具有不同版本 [的机器上获取一段代码 运行 =44=] (3.2.6) 比代码最初构建的那个要多。

我在读取 gzipped-text 文件 line-by-line 时遇到了问题(并根据第一个字符进行处理)。代码(显然是在 python > 3.2.6 中编写的)是

for line in gzip.open(input[0], 'rt'):
    if line[:1] != '>':
        out.write(line)
        continue

    chromname = match2chrom(line[1:-1])
    seqname = line[1:].split()[0]

    print('>{}'.format(chromname), file=out)
    print('{}\t{}'.format(seqname, chromname), file=mappingout)

(对于那些知道的人,这会将 gzip 的 FASTA 基因组文件剥离成 headers(以“>”开头)和序列,并根据此将行处理成两个不同的文件)

我找到了 https://bugs.python.org/issue13989,它指出模式 'rt' 不能用于 python-3.2 中的 gzip.open 并使用以下内容:

import io

with io.TextIOWrapper(gzip.open(input[0], "r")) as fin:
     for line in fin:
         if line[:1] != '>':
             out.write(line)
             continue

         chromname = match2chrom(line[1:-1])
         seqname = line[1:].split()[0]

         print('>{}'.format(chromname), file=out)
         print('{}\t{}'.format(seqname, chromname), file=mappingout)

但是上面的代码不起作用:

UnsupportedOperation in line <4> of /path/to/python_file.py:
read1

如何重写此例程以准确给出我想要的 - 将 gzip 文件 line-by-line 读入变量 "line" 并根据第一个字符进行处理?

编辑:此例程的第一个版本的回溯是 (python 3.2.6):

Mode rt not supported  
File "/path/to/python_file.py", line 79, in __process_genome_sequences  
File "/opt/python-3.2.6/lib/python3.2/gzip.py", line 46, in open  
File "/opt/python-3.2.6/lib/python3.2/gzip.py", line 157, in __init__

第二个版本的回溯是:

UnsupportedOperation in line 81 of /path/to/python_file.py:
read1
File "/path/to/python_file.py", line 81, in __process_genome_sequences

没有进一步的回溯(行数中的额外两行是 import iowith io.TextIOWrapper(gzip.open(input[0], "r")) as fin:

我实际上已经解决了这个问题。

最后我不得不使用shell("gunzip {input[0]}")来确保可以在文本模式下读取压缩文件,然后使用

读取生成的文件
for line in open(' *< resulting file >* ','r'):
    if line[:1] != '>':
        out.write(line)
        continue

    chromname = match2chrom(line[1:-1])
    seqname = line[1:].split()[0]

    print('>{}'.format(chromname), file=out)
    print('{}\t{}'.format(seqname, chromname), file=mappingout)