【问题标题】:Python File Processing One Char At A TimePython文件一次处理一个字符
【发布时间】: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


【解决方案1】:

更新:我想通了!我必须偏移 '>' 字符的第一个跳过(第 2 行)并检查我是否到达了 eof(第 19 行)。 这是我更新的模块(驱动程序在原始帖子中):

def read_one_fasta_entry(fStream) : # Return iterable two-tuples of (pegid, protseq) as long as eof is not reached
    s = fStream.read(1) # Offset skipping '>' char
    while (True) : # Loop to eof
        s = ''
        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 != '>' and s != '') : # Read until next entry (s != '>') or eof (s != '')
            s = fStream.read(1)
            if(s != '>') :
                protseq += s

        if(s == '') : # Check for eof
            yield (pegid, protseq)
            raise StopIteration() # Close generator

        yield (pegid, protseq)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多