【问题标题】:Python lxml gives entities for cyrillic charactersPython lxml 为西里尔字符提供实体
【发布时间】:2020-09-12 19:56:21
【问题描述】:

我正在尝试打印一个包含西里尔字符的字符串。

代码:

from lxml import etree

root = etree.fromstring('<root><child value="тест"/></root>')
child = root.find("child")
print(etree.tostring(child, encoding="unicode"))

...将打印:

<child value="&#x442;&#x435;&#x441;&#x442;"/>

这是我所期待的:

<child value="тест"/>

我怎样才能做到这一点?

【问题讨论】:

    标签: xml lxml python-3.8


    【解决方案1】:

    尝试将method="html" 作为.tostring() 的参数:

    from lxml import etree
    
    root = etree.fromstring('<root><child value="тест"/></root>')
    child = root.find("child")
    print(etree.tostring(child, method="html", encoding="unicode"))
    

    打印:

    <child value="тест"></child>
    

    【讨论】:

    • 工作,谢谢。这总是会给我一个完整的字符串序列化,有什么副作用吗?
    • @usario 阅读docs 它应该可以工作。但是,这一切都取决于输入文档。
    • @Andrey Kesely:输入文档是带有西里尔文字的普通 XML 文档。
    • @usario 那我看没问题。
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2011-12-06
    相关资源
    最近更新 更多