对于不同长度的散列键,列表索引超出范围错误

时间:2012-08-17 19:46:05

标签: python python-3.x python-2.7

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

3 个答案:

答案 0 :(得分: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") 

答案 1 :(得分:1)

if(l[6]):
如果您的阵列要短,

会失败。 访问不属于数组的值将始终导致异常。因此,您应该事先检查数组是否足够长,如下所示:

if len(l) == 7:

答案 2 :(得分:1)

我想第{5}行if(l[6]):应为if(len(l) == 7):

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

的内容
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)