简体   繁体   中英

Python Converting Characters from Unicode to HTML

Hey guys I am trying to convert this in python 2.7.3:

the+c\xf8\xf8n

to the html string:

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.

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. Use urllib.quote :

from urllib import quote

but make sure you encode to UTF-8 first:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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