1. 程式人生 > >python指令碼匯入mysql中文亂碼

python指令碼匯入mysql中文亂碼

實際工作中經常會涉及到指令碼匯入資料到庫中, 如果資料中有中文,搞不好會出現亂碼問題。根據實際工作中的經驗,為了避免亂碼,總結了如下經驗:

以資料庫編碼為utf-8為例

1、把匯入檔案修改成utf-8編碼

ue: 檔案-轉換-asc到utf8

2、指令碼連線資料庫使用utf-8編碼

MySQLdb.connect(host=hostname, user=username, passwd=password, db=dbname, charset='utf8', use_unicode=True)

以上兩個步驟基本可以解決中文亂碼問題