【发布时间】:2019-02-14 10:14:34
【问题描述】:
我无法解决我的 python 代码中的问题,我需要一些提示。
事实是,如果我使用没有for循环的代码,直接用
tree = etree.parse('filename.xml', parser=parser)
一切正常。当我尝试将变量分配给file 以迭代目录内的文件名时,代码挂起,出现以下错误:
Traceback(最近一次调用最后一次):文件“/home/eikaf/test.py”,行 45,在 ragsocemittente = root.find('.//FatturaElettronicaHeader//CedentePrestatore//DatiAnagrafici//Anagrafica//Denominazione') AttributeError: 'NoneType' 对象没有属性 'find'
path = '/home/eikaf/xml'
for dirpath, dirnames, filenames in os.walk(path):
for file in filenames:
#print(file)
#print(filenames)
tree = etree.parse(file, parser=parser)
root = tree.getroot()
ragsocemittente = root.find('.//FatturaElettronicaHeader//CedentePrestatore//DatiAnagrafici//Anagrafica//Denominazione')
if ragsocemittente is None:
ragsocemittente = ''
ragsocemittentecognome = root.find('.//FatturaElettronicaHeader//CedentePrestatore//DatiAnagrafici//Anagrafica//Cognome')
ragsocemittentenome = root.find('.//FatturaElettronicaHeader//CedentePrestatore//DatiAnagrafici//Anagrafica//Nome')
#print("Ragione Sociale Emittente " + ragsocemittentecognome.text + " " + ragsocemittentenome.text)
ragsocemittentegiust = str(ragsocemittentecognome.text + " " + ragsocemittentenome.text).ljust(32)
else:
#print("Ragione Sociale Emittente: " + ragsocemittente.text)
ragsocemittentegiust = str(ragsocemittente.text).ljust(32)
print(ragsocemittente.text)
【问题讨论】:
标签: python xml elementtree