如何将变量值临时保留在内存中并在python中进行比较...]

问题描述 投票:0回答:4

[伙计,我很确定我通过错误的缩进打破了逻辑,但现在我无法解决。请你帮助我好吗?##analyticsNano.py-分析XYZ文件中的'sanity'##

import csv
import sys
import os
import getopt

def main():
    '''
analyzeNano.py -d input-directory

analyzeNano.py analyzes a list of XYZ files inside input-directory. It counts for the number of consequitive DNA samples with identical ID and if it between 96 and 110 it treats it as 'good', otherwise 'bad'.
    input-directory    an input directory where XYZ files are located
    -d    flag for input-directory
At the end it creates 2 files: goodNano.csv and badNano.csv
Note: files that are not in goodNano.csv and badNano.csv have no DNA ID and therefore not listed
'''
    try:
        opts, args = getopt.getopt(sys.argv[1:],'d:')
    except getopt.GetoptError, err:
        print str(err)
        help(main)
        sys.exit(2)

    if len(opts) != 1:
        help(main)
        sys.exit(2)

    if not os.path.isdir( sys.argv[2] ):
        print "Error, ", sys.argv[2], " is not a valid directory"
        help(main)
        sys.exit(2)


    prefix = 'dna'
    goodFiles = []
    badFiles = []

    fileList = os.listdir(sys.argv[2])
    for f in fileList:
        absFile = os.path.join(os.path.abspath(sys.argv[2]), f )
        with open(absFile, 'rb') as csvfile:
            # use csv to separate the fields, making it easier to deal with the
            # first value without hard-coding its size
            reader = csv.reader(csvfile, delimiter='\t')
            match = None
            count = 0

            for row in reader:
                # matching rows
                if row[0].lower().startswith(prefix):

                    if match is None:
                        # first line with prefix..
                        match = row[0]

                    if row[0] == match:
                        # found a match, so increment
                        count += 1

                    if row[0] != match:
                        # row prefix has changed
                        if 96 <= count < 110:
                            # counted enough, so start counting the next
                            match = row[0] # match on this now
                            count = 0 # reset the count
                            goodFiles.append(csvfile.name)
                        else:
                            # didn't count enough, so stop working through this file
                            badFiles.append(csvfile.name)
                            break

                # non-matching rows
                else:
                    if match is None:
                        # ignore preceding lines in file
                        continue
                    else:
                        # found non-matching line when expecting a match
                        break
    else:
        if not 96 <= count < 110:
                    #there was at least successful run of lines
            goodFiles.remove(csvfile.name)

    # Create output files
    createFile(goodFiles, 'goodNano')
    createFile(badFiles, 'badNano')

def createFile(files, fName):
    fileName = open( fName + ".csv", "w" )
    for f in files:
        fileName.write( os.path.basename(f) )
        fileName.write("\n")


if __name__ == '__main__':
    main()

有人可以浏览并指出我在哪里摔坏了?

[伙计,我很确定我通过错误的缩进打破了逻辑,但现在我无法解决它。请你帮助我好吗? ##analyticsNano.py-分析XYZ文件中的'sanity'##import csv ...

python regex
4个回答
0
投票

这是我如何修改您的样式:


0
投票

所有变量都保存在内存中。您想保留最近的匹配并进行比较,并在匹配时计数:


0
投票

根据您的描述,您感兴趣的行与正则表达式匹配:


0
投票

[请忽略我上次检查代码的要求。我自己检查了一下,意识到问题出在格式化上。看起来现在它可以按预期工作并分析目录中的所有文件。再次感谢Metthew。这种帮助是巨大的。我仍然对计算的准确性有些担心,因为在某些情况下它失败了,但它不应该……但我会进行调查。总体而言,非常感谢大家的大力帮助。

© www.soinside.com 2019 - 2024. All rights reserved.