我所有的python源代码都是用utf-8编码的,并且在文件的顶部声明了这个编码。在
但有时unicode字符串前面的u
会丢失。在
示例Umlauts = "üöä"
上面是一个包含非ascii字符的bytestring,这会带来麻烦(UnicodeDecodeError)。在
我尝试了pylint和python -3
,但我没有得到警告。在
我搜索一种在bytestrings中自动查找非ascii字符的方法。在
我的源代码需要支持python2.6和python2.7。在
我得到了一个众所周知的错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)
顺便说一句:这个问题只涉及python源代码,而不是从文件或套接字读取的字符串。在
解决方案
__future__.unicode_literals
当然,您希望使用python来实现这一点!在
请注意,这并不区分裸字符和转义非ascii字符(
fuß
和fu\xdf
)。在相关问题 更多 >
编程相关推荐