Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Python xml ElementTree的字符串源_Python_Xml_Elementtree - Fatal编程技术网

获取Python xml ElementTree的字符串源

获取Python xml ElementTree的字符串源,python,xml,elementtree,Python,Xml,Elementtree,在Python中,如何将ElementTree的源代码作为字符串获取 import xml.etree.ElementTree as ET tree = ET.parse(source) root = tree.getroot() ET.tostring(root) 请注意,source和ET.tostring(doc)的内容之间可能存在格式差异,我知道不是,但我想知道为什么不是: tree.tostring(root) 而不是当前的方式: xml.etree.ElementTree.tos

在Python中,如何将
ElementTree
的源代码作为字符串获取

import xml.etree.ElementTree as ET
tree = ET.parse(source)
root = tree.getroot()
ET.tostring(root)

请注意,
source
ET.tostring(doc)

的内容之间可能存在格式差异,我知道不是,但我想知道为什么不是:

tree.tostring(root)
而不是当前的方式:

xml.etree.ElementTree.tostring(root)

“ElementTree”对象没有属性“tostring”在我尝试时发生this@James:那么你在错误的对象上调用它。是模块有这种方法。@James:如果
tree
ElementTree
(我的错误),那么
ET.tostring(tree)
不起作用,你是对的。相反,使用
root=tree.getroot()
获取树的根,然后调用
ET.tostring(root)
@MartijnPieters Yes!谢谢你指出这一点。我会尽快接受