用unicode写LXML?

2024-06-09 17:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

^{pr2}$

我该如何使用unicode进行写入,或者使用lxml不可能实现这一点?在


Tags: 文件nodetrue节点prettylxmletreeprint
1条回答
网友
1楼 · 发布于 2024-06-09 17:24:03

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

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

来自etree.tostringdocs

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.

相关问题 更多 >