计数文件中的实例,小于或等于

时间:2019-02-27 20:17:25

标签: python

我似乎无法使它正常工作。我有一个txt文件,其中一行中的数字最大为250000,而另一行中的数字为0到4。我想计算多少次该数字小于等于50000且具有对应的数字0。我想将其写入文件。由于某种原因,它无法识别该数字小于等于50000的任何实例。

import sys 
import argparse 
import operator

def main (argv):
    parser = argparse.ArgumentParser(description='Get the variants that are present at least 5% of the time ')
    parser.add_argument('infile', help='file to process')
    parser.add_argument('outfile', help='file to produce')
    args = parser.parse_args()
    results =[]
    c0 = int("0")
    count = 0
    a = int("50000")

    with open(args.infile, "r") as f, open(args.outfile, "w") as of:
        file_in = f.readlines()
        for line in file_in:
            temp = line.split()
            if temp[0]<= a and temp[1]== c0:
                count+=1
                first_trajectory_cluster0 = str(count)
                of.write(first_trajectory_cluster0 + "cluster0" + "\n")

if __name__ == "__main__":
    main(sys.argv)

1 个答案:

答案 0 :(得分:2)

在每种情况下,您都将字符串与整数进行比较:

        temp = line.split()
        if temp[0]<= a and temp[1]== c0:

您需要转换:

        temp = line.split()
        if int(temp[0]) <= a and int(temp[1]) == c0: