将 Pandas 数据集保存到 Excel 时,我遇到了
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe6 in position 0: invalid continuation byte
一些挖掘表明我可以将 3 个 ascii 字符放在一起,结果字符串似乎以 UTF-8 连续字节开头。显然,字符串中没有多字节字符。克服这个问题的最佳方法是什么,以便我的所有数据都被解释为 ASCII 字符?
下面是 Python 代码,它演示了连续字节的表现方式
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> string_from_3_ascii_chars = chr(50) + chr(51) + chr(48)
>>> print(string_from_3_ascii_chars)
230
>>> print(string_from_3_ascii_chars.startswith(str(0xe6)))
True
>>>
在所讨论的示例中,str(0xe6)
采用一个整数0xe6
(230
十进制表示法)并调用repr(object)
它。这会产生 string '230'
。string_from_3_ascii_chars
确实以'230'
. startswith
通过返回来确认这一点True
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句