简体   繁体   中英

Python Unicode Conversion for character 'ñ'

I have to print Unicode of char 'ñ' Eg Input - 'Piñeiro' Output - 'Pi\xf1eiro' (Codecs Used - raw_unicode_escape) Expected - 'Piñeiro'

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?

If you are trying to print out Piñeiro -- 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM