【发布时间】:2020-02-17 14:28:40
【问题描述】:
我正在尝试使用以下代码生成包含标签</documents> 的 XML 文件。
string = "dasdd Wonder asdf new single, “Tomorrow” #URL# | " \
"oiojk asfddsf releases new asdfdf, “gfsg” | " \
"Identity of asfqw who dasd off asdfsdf Mainland jtyjyjui revealed #URL#"
from yattag import Doc, indent
import html, re
doc, tag, text = Doc().tagtext()
with tag('author', lang='en'):
with tag('documents'):
for tweet in string.split(' | '):
with tag('document'):
tweet = html.unescape(tweet)
text('<![CDATA[{}]]'.format(tweet))
result = indent(doc.getvalue(), indentation=' ' * 4, newline='\n')
with open('test.xml', 'w', encoding='utf-8') as f:
f.write(result)
我想在文本周围添加CDATA 标记,但是当我使用Notepad++ 打开生成的文件而不是输出为:
<document><![CDATA[oiojk asfddsf releases new asdfdf, “gfsg”]]></document>
它看起来像(带有 HTML 实体):
<document><![CDATA[oiojk asfddsf releases new asdfdf, “gfsg”]]</document>
我尝试使用HTML 库(html.unescape 行)来丢弃 HTML 实体,但我做不到。
如何解决这个编码问题?
【问题讨论】:
标签: python xml encoding yattag