What I aim to achieve is to:
string like:
Bitte überprüfen Sie, ob die Dokumente erfolgreich in System eingereicht wurden, und löschen Sie dann die tatsächlichen Dokumente.
convert to:
'Bitte \u00FCberpr\u00FCfen Sie, ob die Dokumente erfolgreich in System eingereicht wurden, und l\u00F6schen Sie dann die tats\u00E4chlichen Dokumente.'
and write it in this form to file (which is UTF-8 encoded).
Another solution, not relying on the built-in repr()
but rather implementing it from scratch:
orig = 'Bitte überprüfen Sie, ob die Dokumente erfolgreich in System eingereicht wurden, und löschen Sie dann die tatsächlichen Dokumente.'
enc = re.sub('[^ -~]', lambda m: '\\u%04X' % ord(m[0]), orig)
print(enc)
Differences:
\u
, never any other sequence, whereas repr()
uses about a third of the alphabet (so for example the BEL character will be encoded as \u0007
rather than \a
)\u00FC
rather than \u00fc
)\u
sequences, whereas repr()
turns those into \\u
; could be extended, perhaps to encode \
as \u005C
:
enc = re.sub(r'[^ -[\]-~]', lambda m: '\\u%04X' % ord(m[0]), orig)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With