我有一个文本文件。该文件的每一行有 6 个字段或 7 个字段。
如果一行中有 7 个字段,我将前 6 个字段作为键,将第 7 个字段作为该键的值。
如果一行中有 6 个字段,我将前 5 个字段作为键,将第 6 个字段作为该键的值。
我不知道是否有必要发布整个代码,但为了清晰起见,我发布了整个代码。
我的代码粘贴在下面:
ReqResRS = {}
with contextlib.nested(open(sys.argv[1],'r'), open(sys.argv[2], 'w')) as (inpf, outf):
lines = [l.split() for l in inpf if l.strip()]
for l in lines:
if(l[6]):
myKey = (l[0],l[1],l[2],l[3],l[4],l[5])
myValue = l[6]
if(myKey in ReqResRS):
diff = float(l[6])-float(ReqResRS[myKey]);
if(float(diff) < 0.000008):
ReqResRS[myKey] = myValue
else:
ReqResRS[myKey] = myValue
outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\t" + l[6] + "\n")
else:
ReqResRS[myKey] = myValue
outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\t" + l[6] + "\n")
else:
myKey = (l[0],l[1],l[2],l[3],l[4])
myValue = l[5]
if(myKey in ReqResRS):
diff = float(l[5])-float(ReqResRS[myKey]);
if(float(diff) < 0.000008):
ReqResRS[myKey] = myValue
else:
ReqResRS[myKey] = myValue
outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\n")
else:
ReqResRS[myKey] = myValue
outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\n")