Reputation: 133
I have to print Unicode of char 'ñ' E.g. Input - 'Piñeiro' Output - 'Pi\xf1eiro' (Codecs Used - raw_unicode_escape) Expected - 'Pi\u00f1eiro'
I tried other standard encoding and text encoding codecs from https://docs.python.org/3/library/codecs.html#text-encodings but seems non of this from the table is producing the required output
Any suggestions on which encoding module is required to get required output?
Upvotes: 0
Views: 259
Reputation: 2374
If you are trying to print out Pi\u00f1eiro
-- a string of length 11 -- and not Piñeiro
-- a string of length 7, you could do
In [93]: mytext = "Pi\u00f1eiro"
In [94]: print(mytext)
Piñeiro
In [95]: new_text = "".join(["\\u{:04x}".format(ord(c)) if ord(c) > 0x7f else c for c in mytext])
In [96]: print(new_text)
Pi\u00f1eiro
In [97]: new_text
Out[97]: 'Pi\\u00f1eiro'
How you determine which characters to show like this is up to you. I somewhat arbitrarly picked any characters whose unicode code unit is greater than 007F.
Upvotes: 1