天天看點

(Python)‘NoneType‘ object has no attribute ‘encoding‘

一、

問題介紹:

在寫python web時通路特定網頁,出現了’NoneType’ object has no attribute 'encoding’錯誤。

(Python)‘NoneType‘ object has no attribute ‘encoding‘

二、

檢視檔案mini_frame.py檔案(用于處理業務邏輯的檔案),發現在進行資料庫連接配接時有編碼的設定。

db=pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='stock_db',charset='utf-8')
cursor=db.cursor()
sql="select * from info;"
cursor.execute(sql)
data_from_mysql=cursor.fetchall()
cursor.close()
db.close()
           

解決:

将utf-8給改成utf8即可。

改之後的代碼如下:

db=pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='stock_db',charset='utf8')
cursor=db.cursor()
sql="select * from info;"
cursor.execute(sql)
data_from_mysql=cursor.fetchall()
cursor.close()
db.close()
           

Accept what was and what is, and you’ll have more positive energy to pursue what will be.