Dav*_*542 2 python xml unicode lxml
我有一个数据库字段,它将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.
Run Code Online (Sandbox Code Playgroud)
我当前拥有的文本(self.xml
)包含日语字符等。如何创建节点?
如果您具有unicode,则可以为lxml
以下内容指定utf-8解析器:
utf8_parser = etree.XMLParser(encoding='utf-8')
node = etree.fromstring(self.xml.encode('utf-8'), parser=utf8_parser)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3427 次 |
最近记录: |