1.reload()
python2使用如下,防止中文出現亂碼問題
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
python3下使用sys.setdefaultencoding('utf-8'),編譯時報錯,改爲:(Python3字符串默認編碼unicode, 所以sys.setdefaultencoding也不存在了)
在Python2.x中由於str和byte之間沒有明顯區別,經常要依賴於defaultencoding來做轉換。
在python3中有了明確的str和byte類型區別,從一種類型轉換成另一種類型要顯式指定encoding。可以用以下方法代替:
import importlib,sys
importlib.reload(sys)
2.在Python3.6下安裝MySQL-python模塊
在python2.7中,我們安裝的是 MySqldb或這 MySQL-python,能夠正卻安裝,但在python3中,由於 其使用的擴展 ConfigParser 已經改名爲 configparser,所以在python3 不能正常安裝了
使用mysqlclicent代替
pip install mysqlclient
3.print函數
Python3中print爲一個函數,必須用括號括起來;Python2中print爲class
4.異常處理except的不同:Exception 後需要用as
python 2.7:
try:
mapping_file=request.FILES['mapping-file']
except Exception,e:
print('upload file failed!')
python 3.6
try:
mapping_file=request.FILES['mapping-file']
except Exception as e:
print('upload file failed!')
5.通過import導入模塊和包的區別
python2默認是按照相對路徑導入模塊和包,python3默認則是按照絕對路徑導入,相對導入是用一個 '.' 來聲明的
eg.
from .request_result_data import get_request_result_data