简体   繁体   English

从unicode到ASCII时丢失字符

[英]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字符?

http://dpaste.com/601417/ http://dpaste.com/601417/

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.

相关问题 如何更好地处理涉及unicode字符的编码和解码以及来自ascii的来回 - How do I better handle encoding and decoding involving unicode characters annd going back and forth from ascii 为什么Python在默认编码为ASCII时会打印unicode字符? - Why does Python print unicode characters when the default encoding is ASCII? 如何使用ASCII字符查询Unicode数据库 - How to query unicode database with ascii characters Python - 用 ASCII 字符替换 unicode 表情符号 - Python - replace unicode emojis with ASCII characters Python 打印 unicode 字符而不是 ASCII - Python printing unicode characters instead of ASCII 只包含ASCII字符的UNICODE字符串是否总是等于ASCII字符串? - Will a UNICODE string just containing ASCII characters always be equal to the ASCII string? 在unicode-ascii混合字符串中用空格填充ascii字符 - Padding ascii characters with spaces in a mix unicode-ascii string Unicode 尝试使用非 ascii 字符生成 pdf 时出现解码错误 - Unicode Decoding error when trying to generate pdf with non-ascii characters 如何用 Python 中的 ascii 字符替换 unicode 字符(给出的 perl 脚本)? - How to replace unicode characters by ascii characters in Python (perl script given)? 将全角Unicode字符转换为ASCII字符 - Convert full-width Unicode characters into ASCII characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM