我有一个具有类似特殊结构的数据文件如下:
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)将第二列为1的所有数据与第二列为0的数据分开然后(2)提取数据行他们的第5个数字(例如在第一个数据行中,它将是0.000008)在特定范围内,然后取该行的第6个数字(对于我们的例子,它将是-4.938531),然后取其全部(捕获第6个值)并最终将它们写入新文件中。为此我编写了这段代码虽然不包括第一个任务,但它也无法正常工作。有谁可以帮我调试或建议我一个新的方法?
{{1}}
答案 0 :(得分:0)
你不必删除行,garabage收集器为你做,所以删除'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))