0
이전에 게시했지만 여전히 문제가 있습니다. 스크립트에서이 항목을 저장하면 문제가되는 데이터베이스에 잘못된 결과가 표시됩니다. "Ánimo Inglewood Charter High School"에서는 대신 "xc3x81nimo Inglewood Charter High School"을 표시합니다. 내가 콘솔에 값을 인쇄하고 제대로 보여줍니다.파이썬 스크립트에서 mysql 인코딩 문제가 발생했습니다.
import schoolslib, MySQLdb
cities = schoolslib.getcitylist("ca")
for city in cities:
schools = schoolslib.getschoolstest(city)
data = ['gsId', 'name', 'type', 'gradeRange', 'enrollment', 'gsRating', 'parentRating', 'city', 'state', 'districtId', 'district', 'districtNCESId', 'address', 'phone', 'fax', 'website', 'ncesId', 'lat', 'lon', 'overviewLink', 'ratingsLink', 'reviewsLink', 'schoolStatsLink']
db = MySQLdb.connect(host="localhost", user="schools", passwd="", db="schoolinfo", charset = "utf8", use_unicode =True)
cursor = db.cursor()
schooldata = [0]*23
for school in schools.iter('school'):
for x in range(0, 23):
schooldata[x] = school.find(data[x])
if (schooldata[x] == None) or (schooldata[x].text == None) :
schooldata[x] = ""
else:
schooldata[x] = schooldata[x].text
schooldata[x] = schooldata[x].encode('utf8', "ignore")
print schooldata[x]
cursor.execute("INSERT INTO school (gsId,name,type,gradeRange,enrollment,gsRating,parentRating,city,state,districtId,district,districtNCESId,address,phone,fax,website,ncesId,lat,lon,overviewLink,ratingsLink,reviewsLink,schoolStatsLink) VALUES %r;" % (tuple(schooldata),))
db.commit()