python - 可怕的 python 编码错误,如何阻止它们?

标签 python unicode utf-8 character-encoding

这些一直困扰着我。为什么?看来我的控制台无法处理编码。我认为我的浏览器和文字处理器可以处理它。我没有一个包含所有可能令人窒息的角色的主列表。在不修改我的数据的情况下缓解此问题的最佳方法是什么?

'charmap' codec can't encode character u'\xca'

最佳答案

您需要找出控制台的编码(哪个系统、操作系统等...?) - 不幸的是,'charmap' 是编解码器的一个有点模糊的标识,如 the docs解释一下:

There’s another group of encodings (the so called charmap encodings) that choose a different subset of all unicode code points and how these codepoints are mapped to the bytes 0x0-0xff. To see how this is done simply open e.g. encodings/cp1252.py (which is an encoding that is used primarily on Windows). There’s a string constant with 256 characters that shows you which character is mapped to which byte value.

All of these encodings can only encode 256 of the 65536 (or 1114111) codepoints defined in unicode.

即,它标识一组可能的编解码器,而不是特定的编解码器。

一旦您知道您的控制台支持名为 'foobar' 的编解码器,请更改现在的语句

print(someunicode)

进入

print(someunicode.encode('foobar'))

关于python - 可怕的 python 编码错误,如何阻止它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2846043/

相关文章:

python - Dropbox Python API : File size detection may have failed

C# 按钮文本 Unicode 字符

unicode - Python 3 CGI : how to output raw bytes

python - 在创建 DataFrame 时保持列顺序

Python 循环困惑

java - 将 Unicode (CJK ExtB) 字符转换为 Java/Scala 中的十进制 NCR

ios - 在iOS应用程序开发中,我需要显示一些日语字符,它是2个字节,但我找不到正确的编码来使用

python - 如何将表示 UTF-8 字符的 int 转换为 Unicode 代码点?

Python 非 ascii 字符

python - 当半衰期已知时,如何填充药物不规则时间序列的缺失值