encoding issue with mysql and python -
i have python code pulls info greater schools , stores on our server. works great until hits Á character. have database encoded in utf-8 general code stores above character as: xc1 in database.i attached xml file api generates.
import schoolslib, mysqldb cities = schoolslib.getcitylist("ca") city in cities: schools = schoolslib.getschoolstest(city) info = ['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") cursor = db.cursor() schooldata = [0]*23 school in schools.iter('school'): 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('utf-8', "ignore") cursor.execute("insert 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() xml file: Ánimo inglewood charter high school
when connect() database, assign charset , use_unicode parameters when making mysql connection.
db = mysqldb.connect(host="localhost", user="schools", passwd="", db="schoolinfo", charset = "utf8", use_unicode =true) python mysql
No comments:
Post a Comment