Python 3, how to convert a string into "iso-8859-1" to use in html
When I type the text "gå" in a swedish web page, the html generates the following field: g%E5. That is, the letter a-ring (å) is coded as %开发者_JAVA百科E5. I assume that they are using ISO-8859-1.
If I generate the same html address using a Python 3.0 script the string "gå" is coded as g%C3%A5. Now the letter a-ring(å) is coded as %C3%A5. I assume that Python 3.0 (string)is using utf-8 to code the a-ring in this way.
How can I use Python 3 to generate the html address with g%E5?
I created this Python source file:
#coding: utf-8
print repr(u"gå".encode("ISO-8859-1"))
and got
'g\xe5'
as output.
I'm using Python 2, but it should be the same for Python 3 without the u
before the unicode literal, possibly without the coding line, and with ascii
instead of repr
.
So it should be as simple as specifying "ISO-8859-1"
as the encoding.
Without seeing your exact code I can't tell you where specifically to do that. Check out the docs for whatever you're using for how to set the encoding.
精彩评论