我所有的 python 源代码都以 utf-8 编码,并在文件顶部声明了此编码。
但有时u
缺少 unicode 字符串之前。
例子Umlauts = "üöä"
上面是一个包含非 ascii 字符的字节串,这很麻烦(UnicodeDecodeError)。
我尝试了 pylint,python -3
但我无法收到警告。
我搜索了一种自动方法来查找字节字符串中的非 ascii 字符。
我的源代码需要支持 Python 2.6 和 Python 2.7。
我得到这个众所周知的错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)
顺便说一句:这个问题只是关于 python 源代码,而不是关于从文件或套接字读取的字符串。
解决方案
- 对于需要支持 Python 2.6+ 的项目,我将使用
__future__.unicode_literals
- 对于需要支持 2.5 的项目,我将使用 thg435 的解决方案(模块 ast)