Python 使用xml.etree.ElmentTree时出现类型错误
我试图根据一些数字测试xml文件中的属性。在属性上运行Python 使用xml.etree.ElmentTree时出现类型错误,python,xml,Python,Xml,我试图根据一些数字测试xml文件中的属性。在属性上运行type()时(在本例中为currentNote): 该类型打印为 但是,如果我将最后一行更改为: print type(int(currentNote)) 我明白了: TypeError: int() argument must be a string or a number, not 'NoneType' 不确定发生了什么-似乎我误解了etree解析器返回的内容 currentNote = note.get("default-x")
type()
时(在本例中为currentNote
):
该类型打印为
但是,如果我将最后一行更改为:
print type(int(currentNote))
我明白了:
TypeError: int() argument must be a string or a number, not 'NoneType'
不确定发生了什么-似乎我误解了etree解析器返回的内容
currentNote = note.get("default-x")
get
如果与键不匹配,则默认情况下返回None
不能从非类型
生成整数
In [51]: int(None)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-51-3f3b1d3c7325> in <module>()
----> 1 int(None)
TypeError: int() argument must be a string or a number, not 'NoneType'
[51]中的int(无)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1整数(无)
TypeError:int()参数必须是字符串或数字,而不是“NoneType”
您应该在循环中打印
note
,以确认数据是否符合预期。谢谢,但这也会返回一个值。哎呀,除了第一个!谢谢我只是想试试:把它去掉。我不是指类型,我是指确切的值<代码>打印便笺打印便笺。在循环中获取(“默认-x”)
。啊。我懂了。我明白了-第一个仍然是罪魁祸首。谢谢
In [51]: int(None)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-51-3f3b1d3c7325> in <module>()
----> 1 int(None)
TypeError: int() argument must be a string or a number, not 'NoneType'