Converting xml.etree.ElementTree to string raises "TypeError: argument of type 'int' is not iterable"
Converting xml.etree.ElementTree to string raises "TypeError: argument of type 'int' is not iterable"
我在 Python 3.
中使用 xml.etree.ElementTree
创建了一个简单的 XML 元素
import xml.etree.ElementTree as ElementTree
person = ElementTree.Element("Person", Name="John", Age=18)
我可以使用 Element.get()
从我的元素访问各个属性,没有任何问题。
name = person.get("Name")
age = person.get("Age")
print(name + " is " + str(age) + " years old.")
# output: "John is 18 years old"
但是,如果我尝试使用 .tostring()
将我的元素转换为字符串,我会收到错误消息“TypeError:'int' 类型的参数不可迭代".
print(ElementTree.tostring(person)) # TypeError
为什么我不能在具有整数属性的 xml.etree.ElementTree.Element
上使用 .tostring()
?
完整代码:
import xml.etree.ElementTree as ElementTree
person = ElementTree.Element("Person", Name="John", Age=18)
print(ElementTree.tostring(person)) # TypeError
完整追溯:
Traceback (most recent call last):
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1079, in _escape_attrib
if "&" in text:
TypeError: argument of type 'int' is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/svascellar/.PyCharmCE2017.3/config/scratches/scratch_13.py", line 3, in <module>
print(ElementTree.tostring(person))
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1135, in tostring
short_empty_elements=short_empty_elements)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 776, in write
short_empty_elements=short_empty_elements)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 933, in _serialize_xml
v = _escape_attrib(v)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1102, in _escape_attrib
_raise_serialization_error(text)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1057, in _raise_serialization_error
"cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize 18 (type int)
尽管 Age
是一个数值,但 xml 属性值应该是带引号的字符串:
person = ElementTree.Element("Person", Name="John", Age="18")
或者,如果数据存储为变量,则使用 str()
将其转换为字符串
age = 18
person = ElementTree.Element("Person", Name="John", Age=str(age))
我在 Python 3.
中使用xml.etree.ElementTree
创建了一个简单的 XML 元素
import xml.etree.ElementTree as ElementTree
person = ElementTree.Element("Person", Name="John", Age=18)
我可以使用 Element.get()
从我的元素访问各个属性,没有任何问题。
name = person.get("Name")
age = person.get("Age")
print(name + " is " + str(age) + " years old.")
# output: "John is 18 years old"
但是,如果我尝试使用 .tostring()
将我的元素转换为字符串,我会收到错误消息“TypeError:'int' 类型的参数不可迭代".
print(ElementTree.tostring(person)) # TypeError
为什么我不能在具有整数属性的 xml.etree.ElementTree.Element
上使用 .tostring()
?
完整代码:
import xml.etree.ElementTree as ElementTree
person = ElementTree.Element("Person", Name="John", Age=18)
print(ElementTree.tostring(person)) # TypeError
完整追溯:
Traceback (most recent call last):
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1079, in _escape_attrib
if "&" in text:
TypeError: argument of type 'int' is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/svascellar/.PyCharmCE2017.3/config/scratches/scratch_13.py", line 3, in <module>
print(ElementTree.tostring(person))
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1135, in tostring
short_empty_elements=short_empty_elements)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 776, in write
short_empty_elements=short_empty_elements)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 933, in _serialize_xml
v = _escape_attrib(v)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1102, in _escape_attrib
_raise_serialization_error(text)
File "C:\Users\svascellar\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1057, in _raise_serialization_error
"cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize 18 (type int)
尽管 Age
是一个数值,但 xml 属性值应该是带引号的字符串:
person = ElementTree.Element("Person", Name="John", Age="18")
或者,如果数据存储为变量,则使用 str()
age = 18
person = ElementTree.Element("Person", Name="John", Age=str(age))