UnicodeEncodeError: 'ascii' codec can't encode characters

简介: UnicodeEncodeError: 'ascii' codec can't encode characters

python2代码

import hashlib
hashlib.md5("北京朝阳区").hexdigest()

报错

hashlib.md5("北京朝阳区").hexdigest()
UnicodeEncodeError: 'ascii' codec can't encode characters 
in position 0-11: ordinal not in range(128)

解决如下:


import hashlib
import sys
reload(sys)
sys.setdefaultencoding('gbk')
hashlib.md5("北京朝阳区").hexdigest()

万恶的python2。。。

相关文章
|
4月前
|
编解码 开发者 Python
详解'unicodeescape' codec can't decode bytes in position 16-17: malformed \N character escape
遇到“`unicodeescape` codec can't decode bytes in position X-X: malformed \N character escape”错误时,首先不要慌张。通过逐步检查和修正代码中的Unicode转义序列,大多数情况下都能找到问题的根源并加以解决。有效利用Python的Unicode支持特性,可以优雅地处理各种复杂的字符编码问题。
119 3
|
4月前
|
Python
SyntaxError: Non-ASCII character 与 Cannot decode using encoding "ascii" 错误解决
SyntaxError: Non-ASCII character 与 Cannot decode using encoding "ascii" 错误解决
49 0
|
5月前
|
Python
SyntaxError: Non-ASCII character 与 Cannot decode using encoding "ascii" 错误解决
在Python调试中遇到的两种编码错误:1) "Cannot decode using encoding 'ascii'",此错误发生在处理含有非ASCII字节的字符串时;2) "SyntaxError: Non-ASCII character",当程序文件含中文且未声明编码。解决方法是在脚本开头添加 "# -*- coding: utf-8 -*-" 或 "#coding=UTF-8",告知Python使用UTF-8解析,确保文件实际也以UTF-8编码保存。
56 0
|
数据采集 编解码 Python
UnicodeEncodeErron: ‘gbk‘ codec can‘t encode character ‘ \ue615‘ in position 346139...编码问题解决
不论使用urllib还是使用requests库经常会遇到中文编码错误的问题,我就经常遇到,因为python安装在windows平台上,cmd的默认编码为GBK,所以在cmd中显示中文时会经常提示gbk编码错误。
165 0
|
编解码
UnicodeEncodeError: 'latin-1' codec can't encode characters in position报错问题
UnicodeEncodeError: 'latin-1' codec can't encode characters in position报错问题
1194 1
成功解决SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 6-7: malformed
成功解决SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 6-7: malformed
|
机器学习/深度学习 编解码 Python
成功解决Python3版UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in
成功解决Python3版UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in
成功解决Python3版UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in
|
编解码 Python
UnicodeEncodeError: 'ascii' codec can't encode characters
UnicodeEncodeError: 'ascii' codec can't encode characters
108 0
|
编解码 Python
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128) 最近在用Python处理...
2161 0