【发布时间】:2016-10-17 03:59:14
【问题描述】:
我需要从文件中读取直到某个字符被命中而不存储整行。
我试过这个:
def read_one_fasta_entry(fStream) :
s = '' # temp var
while (s != '>') : # '>' is the char to read until and then discard/skip
fStream.read(1)
但是,这只是将程序发送到具有给定输入的无限循环中: >fig|100226.1.peg.1 SCEND.02c,未知,可疑 CDS,长度:225aa [天蓝色链霉菌 A3(2)] MTGHHESTGPGTALSSDSTCRVTQYQTAGVNARLRLFALLERRACPRARTTTWWPGRSAR WWSWTAWRRLLGVCCVRGRLGRRRDGGERGPGGHRGPGLATARRRSGGATELAVHCADVR QRERADLVRLEGFVRESVLPRAHPHTTARRRVLEVLGEAGSLCARTVNSDEDYILCTLG VGHYDPDDQPPFKDGKPGWQRAGASIWNGSGAACIPHAAIEGPRK
有比上面更多的条目,我需要存储 ID (fig|10026.1.peg.1) 和序列 (MTGHHE...),并且打算使用上述方法一次处理一个字符因为文件是确定性的('>' 在 ID 之前,'' 到结束 ID,']' 在序列之前)但它不起作用。有什么建议吗?
**编辑 我现在已经更新了程序,它似乎大部分都可以工作,但看起来我被一个'>'抵消了 我的模块:
def read_one_fasta_entry(fStream) :
while (True) :
s = ''
while (s != '>') : # Discard first char/extra chars further in the file
s = fStream.read(1)
pegid = ''
while (s != ' ') : # read one char at a time and append to pegid until whitespace
s = fStream.read(1)
pegid += s
protseq = ''
while (s != ']') : # read one char at a time and append to protseq until close square bracket
s = fStream.read(1)
while(s != '>') :
s = fStream.read(1)
protseq += s
yield (pegid, protseq)
司机:
#!/usr/bin/env python3
import sys
import p3mod
f = open(sys.argv[1])
for (pegid,protseq) in p3mod.read_one_fasta_entry(f):
print(pegid,protseq)
f.close()
关于如何跳过第一个“>”的任何想法?我是 python 新手,但是否有相当于“do...while()”循环?看来这里会很有效。
【问题讨论】:
-
您永远不会将变量
s设置为等于您的输入,所以s = ''一直都是。 -
这不是您的实际代码;该方法的参数是
fstream(小写s),稍后您使用fStream(大写S)。请准确地复制/粘贴您的代码。 -
@RNar l0l 哇,太尴尬了……谢谢你指出这一点。
-
@marcelm 抱歉!我已经在原帖中更新了。谢谢指正。
标签: file python-3.x io