gpt4 book ai didi

python - LXML 以 unicode 编写?

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:14 24 4
gpt4 key购买 nike

我目前正在使用 lxml 编写文件。我构建节点,然后使用 etree.tostring(node, pretty_print=True) 将其写入文件。但是,它似乎正在使用 htmlencoding --

<Synopsis>
Abila schlie&#223;lich die ersten sechs Aufgaben zu meistern. Wird der Junge auch
</Synopsis>

为了破译它并将其转换为我想要的格式,我目前正在做:

>>> print HTMLParser.HTMLParser().unescape('Abila schlie&#223;lich die ersten sechs Aufgaben zu meistern. Wird der Junge auch')
Abila schließlich die ersten sechs Aufgaben zu meistern. Wird der Junge auch

我怎么用 unicode 写这个,或者这不可能用 lxml

最佳答案

是的,您可以使用 encoding 参数将编码传递给 etree.tostring 方法:

etree.tostring(node, pretty_print=True, encoding='unicode')

来自 etree.tostring docs :

You can also serialise to a Unicode string without declaration by passing the unicode function as encoding (or str in Py3), or the name 'unicode'. This changes the return value from a byte string to an unencoded unicode string.

关于python - LXML 以 unicode 编写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30656861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com