读取gzip文本文件linebyline以在python3.2.6中进行处理

2024-09-28 22:37:28 发布

您现在位置:Python中文网/ 问答频道 /正文

谈到python,我是一个完全的新手,但我的任务是在一台机器上运行一段代码,该机器的python版本(3.2.6)与该代码最初的版本不同。在

我遇到了一个问题:逐行读取gzip文本文件(并根据第一个字符进行处理)。代码(显然是用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基因组文件分为头文件(在开头加上“>;”)和序列,并根据这一点将这些行处理成两个不同的文件)

我找到了https://bugs.python.org/issue13989,它声明模式'rt'不能用于gzip.open在python-3.2中,可以使用以下代码:

^{pr2}$

但上述代码不起作用:

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

我如何重写这个例程来精确地给出我想要的结果——逐行将gzip文件读入变量“line”并基于第一个字符进行处理?在

编辑:这个例程的第一个版本的回溯是(python3.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 io和{}行


Tags: 文件topath代码inpy版本机器
1条回答
网友
1楼 · 发布于 2024-09-28 22:37:28

我似乎已经解决了这个问题。在

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

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)  

相关问题 更多 >