2014-06-20 2 views
0

나는 더 큰 학교에서 데이터를 가져 와서 우리 서버에 저장하는 파이썬 코드를 가지고 있습니다. 모든 것이 Á 문자에 부딪 힐 때까지 훌륭하게 작동합니다. 나는 UTF-8로 코드화 된 데이터베이스를 가지고있다. 위 코드는 데이터베이스에 xc1이라는 코드를 저장한다. 나는 API가 생성 한 XML 파일을 첨부했다.mysql과 python의 인코딩 문제

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") 
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('utf-8', "ignore") 

    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() 

XML 파일 : MySQL의 연결을 할 때 ANIMO 잉글 우드 헌장 고등학교

답변

0

연결할() 데이터베이스로는, 캐릭터 세트와 use_unicode 매개 변수를 지정합니다.

db = MySQLdb.connect(host="localhost", 
        user="schools", 
        passwd="", 
        db="schoolinfo", 
        charset = "utf8", 
        use_unicode =True) 
+0

닉노가 있어야하는 xc3x81nimo가 계속 표시됩니다. – dcoder50