从 Unicode 字符串创建 xml 节点(不支持编码声明)?
Creating xml node from Unicode string (encoding declaration not supported)?
我有一个数据库字段将 XML 文档存储为 Unicode。但是,当我获取该字段并尝试启动一个 lxml
节点时,出现以下错误:
node = etree.fromstring(self.xml)
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
我目前的文本(self.xml
)包含日语字符等。我将如何创建节点?
如果你有unicode,你可以为lxml
指定utf-8解析器:
utf8_parser = etree.XMLParser(encoding='utf-8')
node = etree.fromstring(self.xml.encode('utf-8'), parser=utf8_parser)
我有一个数据库字段将 XML 文档存储为 Unicode。但是,当我获取该字段并尝试启动一个 lxml
节点时,出现以下错误:
node = etree.fromstring(self.xml)
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
我目前的文本(self.xml
)包含日语字符等。我将如何创建节点?
如果你有unicode,你可以为lxml
指定utf-8解析器:
utf8_parser = etree.XMLParser(encoding='utf-8')
node = etree.fromstring(self.xml.encode('utf-8'), parser=utf8_parser)