将特定xml标记的值插入python元组 下面是xml文件:Results.xml 错误:
请在这方面帮助我您遇到的问题是,在第一个标签中,您没有CMV_FREQ,因此当您尝试使用它时,它会失败:将特定xml标记的值插入python元组 下面是xml文件:Results.xml 错误:,python,xml,Python,Xml,请在这方面帮助我您遇到的问题是,在第一个标签中,您没有CMV_FREQ,因此当您尝试使用它时,它会失败: >>> for combin in root.findall('Combination'): ... for param in combin.getiterator('Parameter'): ... param.get('CMV_FREQ').text ... Traceback (most recent call last): File "&
>>> for combin in root.findall('Combination'):
... for param in combin.getiterator('Parameter'):
... param.get('CMV_FREQ').text
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'text'
另一个问题
peep=rr=[]
使peep==rr
因此,当您更改rr时,peep也将更新
如果您将代码保持原样,那么结果应该是
[((None, '1.0'), 'true'), ((None, '4.0'), 'false')] [((None, '1.0'), 'true'), ((None, '4.0'), 'false')]
而不是:
[((None, '1.0'), 'true')] [((None, '4.0'), 'false')]
peep = ((PEEP, 1.0), true) # I want to check the tag before enter value and corresponding true will be insert into variable
rr = ((CMV_FREQ, 4.0), false)
>>> for combin in root.findall('Combination'):
... for param in combin.getiterator('Parameter'):
... param.get('CMV_FREQ').text
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'text'
for combin in root.findall('Combination'):
peep = []
rr = []
print 1
for param in combin.getiterator('Parameter'):
if param.find('PEEP') is not None: ##This will check if PEEP tag exists
peep.append(((param.get('PEEP'), param.find('PEEP').text), param.find('Result').text))
elif param.find('CMV_FREQ') is not None:##This will check if CMV_FREQ tag exists
rr.append(((param.get('CMV_FREQ'), param.find('CMV_FREQ').text), param.find('Result').text))
print peep, rr
[((None, '1.0'), 'true'), ((None, '4.0'), 'false')] [((None, '1.0'), 'true'), ((None, '4.0'), 'false')]
[((None, '1.0'), 'true')] [((None, '4.0'), 'false')]