Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top