[英]losing characters when going from unicode to ascii
y0 I have this problem that characters that include ñ or ŕ í á ú etc are discarded when I apply y0我有一个问题,当我申请时会丢弃包含ñ或ŕíáú等的字符
text = text.encode('ascii', 'ignore')
to a function that needs the input to be ascii. 到需要输入为ascii的函数。
is there a way to force it to ascii without losing those characters or should I change the function to accept unicode characters? 有没有一种方法可以在不丢失那些字符的情况下将其强制转换为ascii,还是应该更改该函数以接受unicode字符?
The 'ascii'
encoding can't represent the characters you refer to. 'ascii'
编码无法代表您所指的字符。 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. 您必须选择其他编码-可能是'cp850'
或'latin_1'
但随后必须确保输出终端使用相关的代码页来解释8位代码。
On balance, life is easier if you just go Unicode all the way. 总而言之,如果您一路使用Unicode,则生活会更轻松。
Yes, you should go for another encoding, if you need those characters (for example Unicode). 是的,如果需要这些字符(例如Unicode),则应该使用另一种编码。 See ascii table for all chars that are included in ascii. 有关ascii中包含的所有字符,请参见ascii表 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.