简体   繁体   中英

losing characters when going from unicode to ascii

y0 I have this problem that characters that include ñ or ŕ í á ú etc are discarded when I apply

text = text.encode('ascii', 'ignore')

to a function that needs the input to be ascii.

is there a way to force it to ascii without losing those characters or should I change the function to accept unicode characters?

http://dpaste.com/601417/

The 'ascii' encoding can't represent the characters you refer to. You have to choose a different encoding — perhaps 'cp850' or 'latin_1' — but then you have to be sure that your output terminal interprets 8-bit codes using the relevant code page.

On balance, life is easier if you just go Unicode all the way.

Yes, you should go for another encoding, if you need those characters (for example Unicode). See ascii table for all chars that are included in ascii.

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