
类似于这种的: UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 1: ordinal not in range(128)
gbk,utf-8,ascii 搞不转
1 cls1991 2017-12-07 17:13:34 +08:00 贴一下代码 |
2 leavic 2017-12-07 17:17:01 +08:00 换 python3 |
3 p2pCoder 2017-12-07 17:22:16 +08:00 2019 年都要到了,直接 python3 |
4 regicide 2017-12-07 17:22:47 +08:00 import sys reload(sys) sys.setdefaultencoding('utf8') 这个试过没 如果这个不行基本上可以换车上 python3 了 |
爬虫吧,得先识别原编码方式 |
8 Shura 2017-12-07 17:30:09 +08:00 7102 年了,换 Python3 吧 |
9 lhx2008 2017-12-07 17:31:55 +08:00 via Android 文件头标明文件编码 用 decode encode 文本变量前面加个 u |
10 johnsonqrr 2017-12-07 18:02:26 +08:00 PY3,请 |
11 DongDongXie OP @cls1991 装的是 anaconda2.7,环境变量也配置了,就想用个 pip list,结果就给我报错,D:\Anaconda2\Lib\ntpath.py 87 行 报错,result_path = result_path + p_path 就这里。然后加了个“ reload(sys) sys.setdefaultencoding('gbk')”就正常了 # Join two (or more) paths. def join(path, *paths): reload(sys) sys.setdefaultencoding('gbk') """Join two or more pathname components, inserting "\\" as needed.""" result_drive, result_path = splitdrive(path) for p in paths: p_drive, p_path = splitdrive(p) if p_path and p_path[0] in '\\/': # Second path is absolute if p_drive or not result_drive: result_drive = p_drive result_path = p_path continue elif p_drive and p_drive != result_drive: if p_drive.lower() != result_drive.lower(): # Different drives => ignore the first path entirely result_drive = p_drive result_path = p_path continue # Same drive in different case result_drive = p_drive # Second path is relative to the first if result_path and result_path[-1] not in '\\/': result_path = result_path + '\\' result_path = result_path + p_path ## add separator between UNC and non-absolute path if (result_path and result_path[0] not in '\\/' and result_drive and result_drive[-1:] != ':'): 、 |
12 DongDongXie OP 感觉新手很容易如不同编码方式的坑 |
13 ltux 2017-12-07 18:38:57 +08:00 蒙就去学习 |
14 wolong 2017-12-07 19:57:08 +08:00 我在 windows 下命令行里运行 py,也出现过这种情况。 换成直接双击文件运行就好了。 |
15 maidou931019 2017-12-07 20:06:00 +08:00 在 python2 中 str 存的是 bytes 数据,unicode 存的是 unicdoe 编码后的二进制数据, 在 python3 中 str 存的是 unicode 数据,bytes 存的是 bytes 数据 在 python2 中混淆了 bytes 和 unicode 数据,u'hello' + 'hi' 不会报错,结果为一个 unicode 数据 而在 python3 中严格区分了 unicode 和 bytes 数据,字节和字符类型,再混用直接报错,'hello' + b'hi' 不能相加 会报错 |
16 justou 2017-12-07 20:30:28 +08:00 纠结编码问题不要局限于 py2py3 了, 要系统的了解下字符串在计算机中的表示方式以及编码原理, 清楚了原理再结合具体语言到具体的环境去实践并加深理解, 不然即使熟悉了 python 处理编码的方式, 换了个环境又搞蒙了. 不搞清楚原理怎么治都只是治标不治本. 给出一些原理性的参考资料: Computer Systems A Programmer ’ s Perspective: Chapter2, Representing and Manipulating Information http://unicodebook.readthedocs.io/ |
17 Mjz127 2017-12-07 20:39:54 +08:00 请选择用 Python3 :doge |
18 summerwar 2017-12-07 20:49:25 +08:00 当时看了下 py2 然后毅然选择了 py3 |
19 conn4575 2017-12-07 22:59:06 +08:00 via Android 然而即使是 py3,很多库为了兼容 py2,返回的默认字符类型还是 bytes …我只能说这真是一个天冷 |
20 wellsc 2017-12-07 23:01:56 +08:00 via iPhone Python 3.6 也 locale 遇到过好几次字符集错误了 |
21 ethusdt 2017-12-08 10:21:31 +08:00 |