Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
lxml Python包将版权符号更改为HTML实体_Python_Lxml - Fatal编程技术网

lxml Python包将版权符号更改为HTML实体

lxml Python包将版权符号更改为HTML实体,python,lxml,Python,Lxml,我有一个Python程序,可以读取XML文件并修改版本属性。其中一些文件还有版权声明,带有版权符号)。lxml包将它们转换为HTML实体和#169。有没有办法防止这种情况 我尝试使用XMLParser函数的resolve\u entities参数,但没有效果。我已经尝试了Python 2.7和3.6.3。下面的程序是针对Python3的 # coding: utf-8 import os import glob import argparse from lxml import etree xP

我有一个Python程序,可以读取XML文件并修改版本属性。其中一些文件还有版权声明,带有版权符号
。lxml包将它们转换为HTML实体
和#169
。有没有办法防止这种情况

我尝试使用XMLParser函数的
resolve\u entities
参数,但没有效果。我已经尝试了Python 2.7和3.6.3。下面的程序是针对Python3的

# coding: utf-8
import os
import glob
import argparse
from lxml import etree

xParser = etree.XMLParser(strip_cdata=False, resolve_entities=False)
etree.set_default_parser(xParser)

someXML ='<node version="1.0.1"><copyright>Copyright © 2017 by me</copyright></node>'

doc = etree.fromstring(someXML)

print(someXML)
print(etree.tostring(doc))
#编码:utf-8
导入操作系统
导入glob
导入argparse
从lxml导入etree
xParser=etree.XMLParser(strip\u cdata=False,resolve\u entities=False)
etree.set\u default\u解析器(xParser)
someXML='Copyright©2017 by me'
doc=etree.fromstring(someXML)
打印(someXML)
打印(etree.tostring(文档))
它打印出:

<node version="1.0.1"><copyright>Copyright c 2017 by me</copyright></node>
b'<node version="1.0.1"><copyright>Copyright &#169; 2017 by me</copyright></node>'
版权c 2017由我所有
版权©;2017年我的

您可以在转储到字符串时指定
unicode
编码:

etree.tostring(doc, encoding="unicode")
演示:

[1]中的
:来自lxml导入etree
在[2]中:xParser=etree.XMLParser(strip\u cdata=False,resolve\u entities=False)
[3]:someXML='Copyright©2017 by me'
在[4]中:doc=etree.fromstring(someXML,parser=xParser)
在[5]中:打印(someXML)
版权所有©2017由本人所有
在[6]中:打印(etree.tostring(doc,encoding=“unicode”))
版权所有©2017由本人所有

谢谢。当我把这个问题贴在lxml邮件列表上时,我得到了同样的答案。我实际上需要使用write函数,它也有编码参数。新手问题:将整个页面的
“utf-8”
更改为
“unicode”
安全吗?
In [1]: from lxml import etree

In [2]: xParser = etree.XMLParser(strip_cdata=False, resolve_entities=False)

In [3]: someXML ='<node version="1.0.1"><copyright>Copyright © 2017 by me</copyright></node>'

In [4]: doc = etree.fromstring(someXML, parser=xParser)

In [5]: print(someXML)
<node version="1.0.1"><copyright>Copyright © 2017 by me</copyright></node>

In [6]: print(etree.tostring(doc, encoding="unicode"))
<node version="1.0.1"><copyright>Copyright © 2017 by me</copyright></node>