Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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/0/xml/15.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/4/video/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
在python中从ElementTree中删除xml文本_Python_Xml_Decoding_Elementtree - Fatal编程技术网

在python中从ElementTree中删除xml文本

在python中从ElementTree中删除xml文本,python,xml,decoding,elementtree,Python,Xml,Decoding,Elementtree,我试图从XML文档中提取转义节点。节点的原始文本如下所示: <Notes>{&quot;Phase&quot;: 0, &quot;Flipper&quot;: 0, &quot;Guide&quot;: 0, &quot;Sample&quot;: 0, &quot;Triangle8&quot;: 0, &quot;Triangle5&quot;: 0, &

我试图从XML文档中提取转义节点。节点的原始文本如下所示:

<Notes>{&quot;Phase&quot;: 0, &quot;Flipper&quot;: 0, &quot;Guide&quot;: 0,     
&quot;Sample&quot;: 0, &quot;Triangle8&quot;: 0, &quot;Triangle5&quot;: 0,     
&quot;Triangle4&quot;: 0, &quot;Triangle7&quot;: 0, &quot;Triangle6&quot;: 0,     
&quot;Triangle1&quot;: 0, &quot;Triangle3&quot;: 0, &quot;Triangle2&quot;: 0}</Notes> 
我希望得到:

{"Phase": 0, "Flipper": 0, "Guide&quot": 0,     
"Sample": 0, "Triangle8": 0, "Triangle5": 0,     
"Triangle4": 0, "Triangle7": 0, "Triangle6": 0,     
"Triangle1": 0, "Triangle3": 0, "Triangle2": 0}
但是,相反,我得到了:

 {&quot;Phase&quot;: 0, &quot;Flipper&quot;: 0, &quot;Guide&quot;: 0,      
 &quot;Sample&quot;: 0, &quot;Triangle8&quot;: 0, &quot;Triangle5&quot;: 0,   
 &quot;Triangle4&quot;: 0, &quot;Triangle7&quot;: 0, &quot;Triangle6&quot;: 0, 
 &quot;Triangle1&quot;: 0, &quot;Triangle3&quot;: 0, &quot;Triangle2&quot;: 0}

如何获取未转义字符串?

使用
HTMLParser.HTMLParser()

saxutils处理
&,但它不处理


由于
python3.4
可以使用
html.unescape

>>> from html import unescape
>>> unescape('&quot;')
'"'

由于某种原因,我没有在Python
2.7.5
中使用escape for
,但我找到了一个解决方法,使用
replace
函数在XML文件中获得“而不是
,如下所示:

以open(xmlfilename,'w')作为f的
:
f、 写入(myxml.toprettyxml().replace(“,”))

非常正确<代码>“
不需要在XML中引用,因此saxutils模块不需要处理这一点(就像ElementTree)。谢谢。这样做了。总有一天,我将不得不与服务器开发人员交谈,找出服务器为什么首先要转义引号。ElementTree不会取消显示
因为您通常不需要在XML中转义
。出于同样的原因,我的答案是错误的。
In [8]: import HTMLParser    

In [11]: HTMLParser.HTMLParser().unescape('&quot;')
Out[11]: u'"'
In [9]: import xml.sax.saxutils as saxutils

In [10]: saxutils.unescape('&quot;')
Out[10]: '&quot;'    
>>> from html import unescape
>>> unescape('&quot;')
'"'