【问题标题】:Saving a list from __main__从 __main__ 保存列表
【发布时间】:2020-09-14 12:24:23
【问题描述】:

确定的解决方案:应该注意,它应该像迭代它以附加到列表时一样简单......但是,应该注意的是,为什么这对我不起作用的一个大问题是我正在使用和 IDE(Spyder 4.1.3),当我运行代码时,它没有执行我想要的输出。但是,如果我将其保存到 py 脚本中并执行,则通过迭代附加数据不会有问题...

如果使用 IDE 并通过执行整个脚本来运行问题测试以排除 IDE 错误,则可以得出结论。

我有一个类,我想用它来解析一些数据。我成功使用它并创建了一个变量来保存这个类的输出。我可以使用 for 循环查看解析后的数据,但无法将它们保存到实际列表中。有人可以帮我把这些数据拿出来吗?

Class: 

     You can use this code and put it in your own script
class ParseFastQ(object):
    """Returns a read-by-read fastQ parser analogous to file.readline()"""
    def __init__(self,filePath,headerSymbols=['@','+']):
        """Returns a read-by-read fastQ parser analogous to file.readline().
        Exmpl: parser.__next__()
        -OR-
        Its an iterator so you can do:
        for rec in parser:
            ... do something with rec ...
        rec is tuple: (seqHeader,seqStr,qualHeader,qualStr)
        """
        if filePath.endswith('.gz'):
            self._file = gzip.open(filePath)
        else:
            self._file = open(filePath, 'rU')
        self._currentLineNumber = 0
        self._hdSyms = headerSymbols     
    def __iter__(self):
        return self

def __next__(self):
    """Reads in next element, parses, and does minimal verification.
    Returns: tuple: (seqHeader,seqStr,qualHeader,qualStr)"""
    # ++++ Get Next Four Lines ++++
    elemList = []
    for i in range(4):
        line = self._file.readline()
        self._currentLineNumber += 1 ## increment file position
        if line:
            elemList.append(line.strip('\n'))
        else: 
            elemList.append(None)

    # ++++ Check Lines For Expected Form ++++
    trues = [bool(x) for x in elemList].count(True)
    nones = elemList.count(None)
    # -- Check for acceptable end of file --
    if nones == 4:
        raise StopIteration
    # -- Make sure we got 4 full lines of data --
    assert trues == 4,\
           "** ERROR: It looks like I encountered a premature EOF or empty line.\n\
           Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._currentLineNumber)
    # -- Make sure we are in the correct "register" --
    assert elemList[0].startswith(self._hdSyms[0]),\
           "** ERROR: The 1st line in fastq element does not start with '%s'.\n\
           Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._hdSyms[0],self._currentLineNumber) 
    assert elemList[2].startswith(self._hdSyms[1]),\
           "** ERROR: The 3rd line in fastq element does not start with '%s'.\n\
           Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._hdSyms[1],self._currentLineNumber) 
    # -- Make sure the seq line and qual line have equal lengths --
    assert len(elemList[1]) == len(elemList[3]), "** ERROR: The length of Sequence data and Quality data of the last record aren't equal.\n\
           Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._currentLineNumber) 

    # ++++ Return fatsQ data as tuple ++++
    return tuple(elemList)
#

调用这个类做变量:

fastqfile=ParseFastQ('filepath')

然后我想确定它是什么类,因为它不允许我提取解析的 fasqdata

In: type(fastqfile)
Out: __main__.ParseFastQ

这部分代码会让我打印 put all output

for fastq_obj in fastqfile:
    #This is the header
    print(fastq_obj[0])

我试图提取该数据并保存在一个列表中...

seqHeader=[]
for fastq_obj in fastqfile:
    #This is the header
    seqHeader.append(print(fastq_obj[0]))

我也尝试了 .extend,但没有保存任何内容

任何帮助将不胜感激

【问题讨论】:

    标签: python-3.x class namespaces tuples


    【解决方案1】:

    试试这个:

    seqHeader=[]
    for fastq_obj in fastqfile:
        #This is the header
        seqHeader.append(fastq_obj[0])
    
    # look at the seqHeader list
    print(seqHeader)
    

    我真的不明白你为什么要在 append 中传递 print() func。 print() func 不返回任何内容,因此列表中不能追加任何内容。

    【讨论】:

    • 我实际上尝试了 seqHeader.append(fastq_obj[0]) 并且它给了我一个空列表。我使用 print 是因为当我执行 print(fastq_obj[0]) 时,它向我显示了列表。
    • 建议的代码,我之前运行过,但不起作用
    • 我在循环结束时添加了一个打印语句...进行此更改并运行它
    • 你能用底部第二个块显示的输出更新问题吗? (循环内只有打印语句的那个​​)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2021-01-09
    • 1970-01-01
    • 2021-02-13
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多