python的默认编码

python2中,默认使用的是ASCII编码。这个编码和开头的encoding不同之处在于,开头的encoding是对于文件内容的编码,默认编码是一些python方法中默认使用的编码。比如对str进行encode的时候默认先decode的编码,比如文件写操作write的encode的编码等。

python3中,默认使用的是UTF-8编码。

sys.getdefaultencoding() 获取默认编码

import sys
print(sys.getdefaultencoding())  

python2

D:\SoftInstall\Python\Python38\python3.exe E:/PycharmProjects/displayPY3/1.py
ascii

Process finished with exit code 0

python3

D:\SoftInstall\Python\Python38\python3.exe E:/PycharmProjects/displayPY3/1.py
utf-8

Process finished with exit code 0

sys.setdefaultencoding(编码格式) 修改默认编码

下面这个例子,用python2环境

# coding=utf-8
import sys
print(sys.getdefaultencoding())
reload(sys)
sys.setdefaultencoding('utf-8')
print(sys.getdefaultencoding())
s = '中文'
s.encode('utf-8')
print(s)
#等价于s.decode(“utf-8”).encode('utf8')
E:\PycharmProjects\LEDdisplay2\venv\Scripts\python.exe E:/PycharmProjects/LEDdisplay2/2.py
ascii
utf-8
中文

如果上述代码没有修改默认编码,就会使用默认编码ASCII来decode变量s,就会报错

# coding=utf-8
import sys
print(sys.getdefaultencoding())
s = '中文'
s.encode('utf-8')
print(s)
E:\PycharmProjects\LEDdisplay2\venv\Scripts\python.exe E:/PycharmProjects/LEDdisplay2/2.py
ascii
Traceback (most recent call last):
  File "E:/PycharmProjects/LEDdisplay2/2.py", line 5, in <module>
    s.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

Process finished with exit code 1

上述代码为什么需要reload(sys)?请看下面的代码:

# coding=utf-8
import sys
print(sys.getdefaultencoding())
# reload(sys)
sys.setdefaultencoding('utf-8')
print(sys.getdefaultencoding())
s = '中文'
s.encode('utf-8')
print(s)
E:\PycharmProjects\LEDdisplay2\venv\Scripts\python.exe E:/PycharmProjects/LEDdisplay2/2.py
Traceback (most recent call last):
  File "E:/PycharmProjects/LEDdisplay2/2.py", line 5, in <module>
    sys.setdefaultencoding('utf-8')
AttributeError: 'module' object has no attribute 'setdefaultencoding'
ascii

Process finished with exit code 1

reload是用于重新加载之前import的模块。这里需要重新加载sys的原因是:python在加载模块时候删除了sys中的setdefaultencoding方法(可能是出于安全起见),所以需要reload这个sys模块。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值