indexError,在其中搜索

时间:2013-06-06 21:52:57

标签: python

我正在编写一个程序,该程序将读取包含以下数据的CSV文件:

"10724_artifact11679.jpg","H. 3 1/4 in. (8.26 cm)","10.210.114","This artwork is currently on display in Gallery 171","11679"

并将其写入HTML表格。我只想要文件说,在第3位,“这件艺术品没有展出”..但我一直有这组数据的问题

import csv

metlist4 = []

newList = csv.reader(open("v2img_10724_list.csv", 'r')) 
for row in newList:   
    metlist4.append(row)  

artifact_template = """<td>
    <div>
    <img src= "%(image)s" alt = "artifact" />
    <p>Dimensions: %(dimension)s </p>
    <p>Accession #: %(accession)s </p>
    <p>Display: %(display)s </p>
    <p>index2: %(index2)s </p>
    </div>
    </td>"""

html_list = []

count = 5794
for artifact in metlist4:
        if artifact[3] in ["This artwork is not on display"]:
                artifactinfo = {}
                artifactinfo["image"]=artifact[0]
                artifactinfo["dimension"]=artifact[1]
                artifactinfo["accession"]=artifact[2]
                artifactinfo["display"]=artifact[3]
                artifactinfo["index2"]=count    
                count = count + 1
                html_list.append(artifact_template % artifactinfo)
         else:
                pass

f = open("v3display_test.txt", "w")
f.write("\n".join(html_list))
f.close()  

我收到此错误,但只有在我运行整个metlist4时才会...

  File "/Users/Rose/Documents/workspace/METProjectFOREAL/src/no_display_Met4.py", line 34, in <module>
    if artifact[3] in ["This artwork is not on display"]:
IndexError: list index out of range

如果我只运行一个部分,例如metlist4 [0:500],则不会发生错误。任何想法或建议将不胜感激!!谢谢!

1 个答案:

答案 0 :(得分:3)

至少有一个行没有第4个元素。也许这条线是空的。

测试长度,并打印行进行测试:

if len(artifact) < 4:
    print 'short row', artifact

如果是空行,请跳过它:

if not artifact: continue

您正在使用大量详细和冗余的代码;当您可以直接遍历csv.reader()对象时,无需构建单独的列表,也无需添加空的else: pass块。

惯用语Python代码将是:

artifact_template = """<td>
    <div>
    <img src= "%(image)s" alt = "artifact" />
    <p>Dimensions: %(dimension)s </p>
    <p>Accession #: %(accession)s </p>
    <p>Display: %(display)s </p>
    <p>index2: %(index2)s </p>
    </div>
    </td>"""

html_list = []

fields = 'image dimension accession display'.split()

with open("v2img_10724_list.csv", 'rb') as inputfile:
    reader = csv.DictReader(inputfile, fields=fields, restval='_ignored')
    for count, artifact in enumerate(reader, 5794):
         if artifact and artifact['display'] == "This artwork is not on display":
              artifactinfo["index2"] = count    
              html_list.append(artifact_template % artifact)

使用csv.DictReader()代替每行创建字典,使用with语句确保文件在完成时关闭,enumerate()使用起始值跟踪count }}