0

我有一个文本文件。该文件的每一行有 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")
4

3 回答 3

3

我认为您想更改if l[6]if len(l) == 7

但是这种事情应该比你做的更容易(从上面的代码中,你应该需要根据你的数据文件有多少列来特殊情况):

myValue = l[-1] 
myKeys = l[:-1]  #tuple(l[:-1]) if you must ...
try:
    diff = float(myValue)-float(ReqResRS[myKey])
    ReqResRS[myKey] = myValue
    if(float(diff) >= 0.000008):
        outf.write("\t".join(l) + "\n") 
except KeyError:
    ReqResRS[myKey] = myValue
    outf.write("\t".join(l) + "\n") 
于 2012-08-17T19:48:00.933 回答
1
if(l[6]):

如果您的阵列太短,则会失败。访问不属于数组的值总是会导致异常。因此,您应该事先检查数组是否足够长,如下所示:

if len(l) == 7:
于 2012-08-17T19:47:52.947 回答
1

我想if(l[6]):应该if(len(l) == 7):在第5行

请注意,您还可以编写类似的内容

try:
    myKey = (l[0], l[1], l[2], l[3], l[4], l[5])
    myValue = l[6]
except IndexError:
    myKey = (l[0], l[1], l[2], l[3], l[4])
    myValue = l[5]

if(myKey in ReqResRS):
    diff = float(myValue) - float(ReqResRS[myKey]);
    if(float(diff) < 0.000008):
            ReqResRS[myKey] = myValue
    else:
            ReqResRS[myKey] = myValue
            outf.write('{}\t{}\n'.format("\t".join(myKey), myValue)
else:
    ReqResRS[myKey] = myValue
    outf.write('{}\t{}\n'.format("\t".join(myKey), myValue)
于 2012-08-17T19:51:49.560 回答