我有一堆从文本文件中提取到 MYSQL 表的英语句子。这就是我在 MYSQL 中创建表的方式:
create table sentences ( ID int NOT NULL AUTO_INCREMENT , sentence varchar (255) , primary key (ID) ) character set = utf8;
这是我的python脚本
from bs4 import BeautifulSoup as b
import sys
from fixsentence import *
import MySQLdb as db
bound = sys.argv[1]
con = db.connect('localhost' , 'root' , 'ayrefik1' , 'knowledgebase2')
curs = con.cursor()
def gettext(file):
temp_file = open(file)
soup = b(temp_file)
list = get_sentences(soup.get_text())
for x in list:
curs.execute('SET NAMES utf8;')
curs.execute('insert ignore into sentences (sentence) values (%s);', (x))
con.commit()
gettext(bound)
我以这种方式在文件上运行脚本
python wikitext.py test
因此,即使我指定该表应该能够处理 UTF-8 中的所有字符,我仍然收到此错误:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 86-87: ordinal not in range(256)