[英]Reading gzipped text file line-by-line for processing in python 3.2.6
對於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)
(對於那些知道,這條帶將FASTA基因組文件壓縮成標題(在開始時帶有“>”)和序列,並根據此處理行分為兩個不同的文件)
我找到了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”並根據第一個字符進行處理?
編輯:從這個例程的第一個版本回溯是(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 io
和with io.TextIOWrapper(gzip.open(input[0], "r")) as fin:
lines
我實際上似乎已經解決了這個問題。
最后我不得不使用shell("gunzip {input[0]}")
來確保可以在文本模式下讀入gunzipped文件,然后使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.