【问题标题】:How to parse xml files containing symbols like '&, <>,~' using python [duplicate]如何使用python解析包含'&,<>,~'等符号的xml文件
【发布时间】:2016-07-20 04:49:39
【问题描述】:

我在阅读以下 xml 文件时收到TypeError: Type 'NoneType' cannot be serialized.

<root>
 <sub_component> 
     Hii & heloo <> 
 </sub_component>
</root>

我用来编码的代码如下

from lxml import etree
parser = etree.XMLParser(recover=True) # recover from bad characters.
root = etree.fromstring(file_path, parser=parser)
print etree.tostring(root)

【问题讨论】:

  • 那不是有效的 XML
  • 不幸的是,我有一个这样的文件,需要对其进行处理。有什么办法吗?

标签: python xml


【解决方案1】:

你可以使用BeautifulSoup:

from bs4 import BeautifulSoup

soup= BeautifulSoup(xml_string, 'html.parser')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多