我目前有一个Python脚本执行get请求并打印出xml格式的数据。如何将此输出转储到xml文件以进行解析?
答案 0 :(得分:1)
假设您有一个包含xml数据的字符串变量,您可以执行以下操作:
with open("output.xml", "w") as f:
f.write(xmlstr)
答案 1 :(得分:0)
如果您使用Elementtree
import xml.etree.cElementTree as ET
root = ET.Element("root")
doc = ET.SubElement(root, "doc")
ET.SubElement(doc, "field1", name="blah").text = "some value1"
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"
tree = ET.ElementTree(root)
tree.write("filename.xml")