将字符串添加到数据库Python后错误的字母

时间:2022-04-14 07:44:29

after adding about 6k of lines into my mySQL database through Python, some letters are wrong ( polish letters), like Å‚ for ł and ż for ż and others. How can I repair that? My python code is:

通过Python在我的mySQL数据库中添加大约6k行之后,有些字母错误(抛光字母),如Å,ł和ż代表ż等。我该怎么修呢?我的python代码是:

 # -*- coding: utf-8 -*-
import MySQLdb
import string
import codecs
file = codecs.open("----", 'r', 'utf-8')
db = MySQLdb.connect(host="-----", port=3306, user="-----", passwd="------", db="----")
cursor = db.cursor()
for line in file:
        lines = string.replace(line, "'", '"')
        cursor.execute("INSERT INTO Logs (Text) VALUE ('%s')"% lines.encode("utf-8"))
db.close()
print("done")

and after running this code, it works normally , but in PhpMyAdmin, there is wrong letters. Coding in Database is UTF-8, file is in ANSI as UTF-8 (from notepad++). Any Help?

运行此代码后,它正常工作,但在PhpMyAdmin中,有错误的字母。数据库中的编码是UTF-8,文件是ANSI的UTF-8(来自记事本++)。任何帮助?

1 个解决方案

#1


2  

Specify the charset in MySQLdb.connect():

在MySQLdb.connect()中指定charset:

db = MySQLdb.connect(host="-----", port=3306, user="-----",
    passwd="------", db="----", charset='utf8')

#1


2  

Specify the charset in MySQLdb.connect():

在MySQLdb.connect()中指定charset:

db = MySQLdb.connect(host="-----", port=3306, user="-----",
    passwd="------", db="----", charset='utf8')