被 Python 编码搞蒙逼

2017-12-07 17:08:04 +08:00
 DongDongXie

类似于这种的: UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 1: ordinal not in range(128)

gbk,utf-8,ascii 搞不转

3362 次点击
所在节点    程序员
21 条回复
cls1991
2017-12-07 17:13:34 +08:00
贴一下代码
leavic
2017-12-07 17:17:01 +08:00
换 python3
p2pCoder
2017-12-07 17:22:16 +08:00
2019 年都要到了,直接 python3
regicide
2017-12-07 17:22:47 +08:00
import sys
reload(sys)
sys.setdefaultencoding('utf8')
这个试过没 如果这个不行基本上可以换车上 python3 了
marcong95
2017-12-07 17:27:03 +08:00
@p2pCoder 2018 都没到,亲你是穿越了?
p2pCoder
2017-12-07 17:27:40 +08:00
@marcong95 。。。。中午没睡,今天下午有点飘
livexia
2017-12-07 17:29:44 +08:00
爬虫吧,得先识别原编码方式
Shura
2017-12-07 17:30:09 +08:00
7102 年了,换 Python3 吧
lhx2008
2017-12-07 17:31:55 +08:00
文件头标明文件编码
用 decode encode 文本变量前面加个 u
johnsonqrr
2017-12-07 18:02:26 +08:00
PY3,请
DongDongXie
2017-12-07 18:08:21 +08:00
@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:] != ':'):
DongDongXie
2017-12-07 18:08:52 +08:00
感觉新手很容易如不同编码方式的坑
ltux
2017-12-07 18:38:57 +08:00
蒙屄就去学习
wolong
2017-12-07 19:57:08 +08:00
我在 windows 下命令行里运行 py,也出现过这种情况。
换成直接双击文件运行就好了。
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' 不能相加 会报错
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/
Mjz127
2017-12-07 20:39:54 +08:00
请选择用 Python3 :doge
summerwar
2017-12-07 20:49:25 +08:00
当时看了下 py2 然后毅然选择了 py3
conn4575
2017-12-07 22:59:06 +08:00
然而即使是 py3,很多库为了兼容 py2,返回的默认字符类型还是 bytes …我只能说这真是一个天冷
wellsc
2017-12-07 23:01:56 +08:00
Python 3.6 也 locale 遇到过好几次字符集错误了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/412857

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX