Python将字符从Unicode转换为HTML

Python将字符从Unicode转换为HTML,python,python-2.7,Python,Python 2.7,嘿,伙计们,我正试图在python 2.7.3中转换这一点: the+c\xf8\xf8n 要删除html字符串,请执行以下操作: the+c%C3%B8%C3%B8n 这是原始的c\xf8\xf8n,但我确实使用了替换来使用+而不是空格 我不完全确定后一种约定是什么,我会使用字符串替换,但约定会因不同的字符而改变 想法?谢谢大家你们是URL编码,不是HTML。使用urllib.quote: from urllib import quote 但请确保首先编码到UTF-8: quote(in

嘿,伙计们,我正试图在python 2.7.3中转换这一点:

the+c\xf8\xf8n
要删除html字符串,请执行以下操作:

the+c%C3%B8%C3%B8n
这是原始的
c\xf8\xf8n
,但我确实使用了替换来使用+而不是空格

我不完全确定后一种约定是什么,我会使用字符串替换,但约定会因不同的字符而改变

想法?谢谢大家

你们是URL编码,不是HTML。使用
urllib.quote

from urllib import quote
但请确保首先编码到
UTF-8

quote(inputstring.encode('utf8'))
这将明确引用
+
;如果您想将其作为空格字符,则需要将其标记为安全:

quote(inputstring.encode('utf8'), '+')
后一种形式给出:

>>> quote(inputstring.encode('utf8'), '+')
'the+c%C3%B8%C3%B8n'

啊!我完全在URL编码空间,我会尝试这一点,并张贴,谢谢