Python2使用Python3不再使用系列(持续更新)~

1.reload()函数

Python 2:

reload()函数封装在sys模块中,使用时:

import sys
reload()
Python 3:

reload()不再封装在sys模块中,而是importlib,使用方式如下:

import importlib
import yourfile
reload('yourfile')
注意:使用reload()函数时需要import你的文件

参考文章:
https://blog.csdn.net/heifan2014/article/details/79266471

2. sys.setdefaultencoding()函数

Python2:

使用该函数进行utf-8编码

importsys
reload(sys)
sys.setdefaultencoding("utf-8")
Python3:

若在python3中使用该函数则会报错:

AttributeError: module 'sys' has no attribute 'setdefaultencoding

Python3字符串默认编码unicode,因此sys.setdefaultencoding就不存在了

参考文章:
https://blog.csdn.net/fly910905/article/details/74922378

3. raw_input()函数

Python3将raw_input和input进行整合成了input,去除了raw_input()函数

其接受任意输入, 将所有输入默认为字符串处理,并返回字符串类型

原文:
https://blog.csdn.net/weixin_40569991/article/details/81053628

猜你喜欢

转载自blog.csdn.net/FannieCream/article/details/92011088
今日推荐