1

I need to convert a string into a different encoding which I believe is Windows1252, not sure though.

The characters would be:

%E4 = ä
%F6 = ö

I tried urlencode, quote, quote_plus, encode... nothing did the trick.

Maybe I am using the wrong encoding?

'mausebär'.encode('Windows-1252')

Should become:

'mauseb%e4r'

Any ideas how to encode with the same characterset?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
merlin
  • 2,717
  • 3
  • 29
  • 59

1 Answers1

3

You're looking for URL quoting with CP-1252 (AKA Windows-1252) encoding. You can use urllib.parse.quote with the encoding parameter:

>>> import urllib.parse
>>> urllib.parse.quote('ä', encoding='cp1252')
'%E4'
>>> urllib.parse.quote('ö', encoding='cp1252')
'%F6'

Main question about URL quoting: How to percent-encode URL parameters in Python?

wjandrea
  • 28,235
  • 9
  • 60
  • 81