テキスト ファイルから 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)