【发布时间】:2019-12-17 18:44:56
【问题描述】:
我想以这种格式将数据保存在xml中
<posts>
<row Id="1" PostTypeId="1" AcceptedAnswerId="13"
CreationDate="2010-09-13T19:16:26.763" Score="297" ViewCount="472045"
Body="<p>This is a common question by those who have just rooted their phones. What apps, ROMs, benefits, etc. do I get from rooting? What should I be doing now?</p>
"
OwnerUserId="10"
LastEditorUserId="16575" LastEditDate="2013-04-05T15:50:48.133"
LastActivityDate="2018-05-19T19:51:11.530"
Title="I've rooted my phone. Now what? What do I gain from rooting?"
Tags="<rooting><root-access>"
AnswerCount="3" CommentCount="0" FavoriteCount="194"
CommunityOwnedDate="2011-01-25T08:44:10.820" />
</posts>
我试过这个,但我不知道如何以上述格式保存:
import xml.etree.cElementTree as ET
root = ET.Element("posts")
row = ET.SubElement(root, "row")
ET.SubElement(row, questionText = "questionText").text = questionText
ET.SubElement(row, votes = "votes").text = votes
ET.SubElement(row, tags = "tags").text = tags
tree = ET.ElementTree(root)
tree.write("data.xml")
【问题讨论】:
-
你能分享输入数据的样子吗?
-
其实我是从网上报废的,想保存在 xml 中。
-
您的代码至少有几个缩进错误 - 请修复。显示你得到的输出。将您的代码设为minimal reproducible example - 目前由于识别问题,我无法复制/粘贴并运行它以获得任何输出。