您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-查找unicode / ascii问题

Python-查找unicode / ascii问题

答案实际上很简单:一旦从文件中读取数据,就使用文件的编码将其转换为unicode,并处理UnicodeDecodeError异常:

try:
        # decode using utf-8 (use ascii if you want)
        unicode_data = str_data.decode("utf-8")
except UnicodeDecodeError, e:
        print "The error is there !"

这将使您免于许多麻烦;您不必担心多字节字符编码,并且外部库(包括xlwt)仅在需要编写“正确的东西”时才做。

Python 3.0将强制要求指定字符串的编码,因此现在开??始这样做是个好主意。

python 2022/1/1 18:44:49 有308人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶