如何在我的特殊数据结构中提取一系列数据 python

how to extract a range of data in my special data structure in python

我有一个数据文件,其特殊结构如下:

#F A 1 1 1 3 3 2 2 1 0.002796 0.000005 0.000008 -4.938531 1.039083 3 1 0.002796 0.000005 0.000007 -4.938531 1.039083 4 0 0.004961 -0.000008 -0.000002 -4.088534 0.961486 5 0 0.004961 0.000006 -0.000002 -4.079798 0.975763

第一列只是描述(无需考虑),我想(1)将第二列为1的所有数据与第二列为0的数据分开,然后(2)提取数据行的第 5 个数字(例如,在第一条数据行中,它将是 0.000008)在特定范围内,然后取该行的第 6 个数字(对于我们的示例,它将是 -4.938531),然后取所有的平均值它们(捕获第 6 个值)并最终将它们写入一个新文件。为此,我编写了这段代码,虽然不包括第一个任务,但它也不起作用。任何人都可以帮我调试或建议我一个新方法吗?

A=0.0 #to be used for separating data as mentioned in the first task
B=0.0 #to be used for separating data as mentioned in the first task
with open('inputdatafile') as fin, open('outputfile','w') as fout:
 for line in fin:
    if line.startswith("#"):
        continue
    else:
        col = line.split()
        6th_val=float(col[-2])
        2nd_val=int(col[1])
        if (str(float(col[6])) > 0.000006 and str(float(col[6])) < 0.000009):
            fout.write(" ".join(col) + "\n")
        else:
            del line
  1. python中的变量名不能以数字开头,所以将6th_val改为val_6,2nd_val改为val_2。
  2. str(float(col[6])) 生成字符串,无法与 float '0.000006' 进行比较,因此将任何 str(float(...)) > xxx 更改为 float(... ) > xxx .
  3. 您不必删除行,垃圾收集器会为您完成,因此请删除 'del line'

    A=0.000006
    B=0.000009
    S=0.0
    C=0
    with open('inputdatafile') as fin, open('outputfile','w') as fout:
      for line in fin:
        if line.startswith("#"):
          continue
        else:
          col = line.split()
          if col[1] == '1':
            val_6=float(col[-2])
            val_5=int(col[-3])
            if val_5 > A and val_5 < B:
              fout.write(" ".join(col) + "\n")
              s += val_6
              c += 1
      fout.write("Average 6th: %f\n" % (S/C))