简体   繁体   English

Python将字符从Unicode转换为HTML

[英]Python Converting Characters from Unicode to HTML

Hey guys I am trying to convert this in python 2.7.3: 大家好,我想在python 2.7.3中进行转换:

the+c\xf8\xf8n

to the html string: 到html字符串:

the+c%C3%B8%C3%B8n

It was original the c\\xf8\\xf8n but I did use a replace to use a + instead of the space. 它原来是c\\xf8\\xf8n但是我确实使用了替换来使用+而不是空格。

I'm not entirely sure what convention the latter is I would use string replace but the convention changes by the different characters.. 我不完全确定哪种约定是后者,我会使用字符串替换,但是约定会因不同的字符而改变。

Thoughts? 有什么想法吗? Thanks guys 多谢你们

You are URL encoding, not HTML. 您是URL编码,而不是HTML。 Use urllib.quote : 使用urllib.quote

from urllib import quote

but make sure you encode to UTF-8 first: 但请确保首先编码为UTF-8

quote(inputstring.encode('utf8'))

This will quote the + explicitly; 这将明确引用+ if you meant that to be a space character, you need to mark that as safe: 如果您要成为空格字符,则需要将其标记为安全:

quote(inputstring.encode('utf8'), '+')

The latter form gives: 后一种形式给出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM