我仍然是python和xml的初学者。以下错误让我烦恼不已。以下是我在执行以下代码时得到的错误(错误):
Traceback (most recent call last):
File "path", line 66, in <module>
print slidename[a].childNodes[0].nodeValue
IndexError: list index out of range
来自此代码:
for object in os.listdir(my_directory):
print (object)
if '.zip' in object:
#print (object)
new = os.path.splitext(object)
#print (new[0])
file_name.append(new[0])
os.mkdir(my_directory+new[0], 0o777)
with zipfile.ZipFile(my_directory+new[0]+new[1], "r") as z:
z.extractall(my_directory+new[0])
count = 0
for len_num in range(0,len(file_name)):
for doc in os.listdir(my_directory+file_name[len_num]+new_directory):
if '.xml' in doc:
#print doc
slide_name = os.path.splitext(doc)
#print slide_name
print ((file_name)[len_num])+'>>>'+((slide_name)[0])
file= xml.dom.minidom.parse(my_directory+((file_name)[len_num])+new_directory+doc)
tagname = file.getElementsByTagName('a:t')
#print tagname
for a in range(0,len(tagname)):
print tagname[a].childNodes[0].nodeValue
提前致谢!!
答案 0 :(得分:3)
您正在解析不具有具有特定slidename[a]
元素的子节点的XML。你可能想跳过那个。
您没有充分利用Python循环。当你可以直接遍历列表本身时,不需要来循环遍历索引:
for a in tagname:
try:
print a.childNodes[0].nodeValue
except IndexError:
print 'No childNodes for this element'