Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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/email/3.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中的字符串_Python_Regex_String_Substring - Fatal编程技术网

要删除的正则表达式&;引用' 来自Python中的字符串

要删除的正则表达式&;引用' 来自Python中的字符串,python,regex,string,substring,Python,Regex,String,Substring,我使用以下代码从RSS提要获取结果: try: desc = item.xpath('description')[0].text if date is not None: desc =date +"\n"+"\n"+desc except: desc = None 但有时描述在提要中包含的unicode html字符很少,如下所示: XML中的文本看起来像“and with”和其他&……;内容 在显示内容时,我不希望显示这些内容。是否有任何正则表达式可以删除HTML标记。我

我使用以下代码从RSS提要获取结果:

try:  
desc = item.xpath('description')[0].text
if date is not None:
    desc =date +"\n"+"\n"+desc
except:
    desc = None
但有时描述在提要中包含的unicode html字符很少,如下所示:

XML中的文本看起来像“and with”和其他&……;内容

在显示内容时,我不希望显示这些内容。是否有任何正则表达式可以删除HTML标记。

我使用了一种称为“不可回避XML”的方法,不知道它是否对您有帮助

见:

从xml.sax.saxutils导入unescape
unescape(“&;”)
'< & >'
unescape(“&apos;”,{“&apos;:”,“:”})
'\' "'
编辑


刚刚看到这一点,可能是相互测试的(未测试):

XML解析器应该转换这些内容,例如
etree.XML(“'hi';”).xpath('/a')[0]。text
给出
'“嗨\'
请给我们看一下您试图解析的XML。@a'r:对不起,我在回答时错过了您的第一条评论;你是第一个。很抱歉,我不能使用第三方库,因为这将在嵌入式软件中运行ssytem@Subhen当前位置我刚刚发现了另一种方法:查看这个问题的第一个答案:但我没有尝试。
from xml.sax.saxutils import unescape

unescape("&lt; &amp; &gt;")

'< & >'




unescape("&apos; &quot;", {"&apos;": "'", "&quot;": '"'})

'\' "'