質問

I've a problem with some strings in python.

I want to convert a string like this: "sanità" , in this one: "sanit%e0" in order to use it into an URL.

I tried everything on internet using different library (like .encode and .decode) but none of them worked.

Thank you!

役に立ちましたか?

解決

You're looking for urllib.quote method:

urllib.quote(u'sanità'.encode('latin1'))
'sanit%E0'

Notice that the output of urllib.quote will be encoding dependent. For example with a cp437 encoded string the output would be sanit%85. But well, urllib.quote will do that kind of scaping.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top