2013-02-25 10 views
6

Yeni desteklenmez ama sadece işe yaramaz :(TypeError: Unicode deşifre sqlite veritabanına düzgün çözmek için ayrıştırıcı elde etmeye çalışıyorlar .... piton için

# coding: utf8 
from pysqlite2 import dbapi2 as sqlite3 
import urllib2 
from bs4 import BeautifulSoup 
from string import * 


conn = sqlite3.connect(':memory:') 
cursor = conn.cursor() 

# # create a table 
def createTable(): 
    cursor.execute("""CREATE TABLE characters 
         (rank INTEGER PRIMARY KEY, word TEXT, definition TEXT) 
        """) 


def insertChar(rank,word,definition): 
    cursor.execute("""INSERT INTO characters (rank,word,definition) 
         VALUES (?,?,?)""",(rank,word,definition)) 


def main(): 
    createTable() 

    # u = unicode("辣", "utf-8") 

    # insertChar(1,u,"123123123") 

    soup = BeautifulSoup(urllib2.urlopen('http://www.zein.se/patrick/3000char.html').read()) 
    # print (html_doc.prettify()) 

    tables = soup.blockquote.table 

    # print tables 

    rows = tables.find_all('tr') 
    result=[] 
    for tr in rows: 
     cols = tr.find_all('td') 
     character = [] 
     x = cols[0].string 
     y = cols[1].string 
     z = cols[2].string 
     xx = unicode(x, "utf-8") 
     yy = unicode(y , "utf-8") 
     zz = unicode(z , "utf-8") 
     insertChar(xx,yy,zz) 

    conn.commit() 

main() 

ben takip almaya devam hata: muhtemelen bir şey yapıyorum TypeError: decoding Unicode is not supported

WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER. 
Traceback (most recent call last): 
    File "sqlitetestbed.py", line 64, in <module> 
    main() 
    File "sqlitetestbed.py", line 48, in main 
    xx = unicode(x, "utf-8") 


Traceback (most recent call last): 
File "sqlitetestbed.py", line 52, in <module> 
main() 
File "sqlitetestbed.py", line 48, in main 
insertChar(x,y,z) 
File "sqlitetestbed.py", line 20, in insertChar 
VALUES (?,?,?)""",(rank,word,definition)) 
pysqlite2.dbapi2.IntegrityError: datatype mismatch 

şu gerçekten aptal ... :(yanlış ne yapıyorum söyle ... teşekkürler

+1

Ahhhh, unicode. Her pythonun kanadı gelişir, neden 'unicode() 'kullanıyorsunuz? "u" use "kullanın ve pythonic şekilde yapın. – Amelia

+0

@Hiroto, kodu değişmezler ile değiştirmeyi mi öneriyorsunuz? – wRAR

+0

@wRAR Ben ekleme için yorumlanan parçaları kastedildi. 'Unicode() 'kodunun kendisi gereksizdir ve – Amelia

cevap

6

!zaten , cols[0].string alanı unicode (yalnızca documented olarak) içeriyor.

+0

tamam .... bu yüzden bir 'traceback (son En son çağrı) alıyorum ... şimdi yerine xx x kullanılan Dosya "sqlitetestbed.py", satır 48, ana insertChar (x, y, z) Dosya "sqlitetestbed.py", satır 20, insertChar VALUES (?,?,?) "" ", (Rank, word, definition)) 'Neden bu? – user805981

+0

@ user805981 tam mesaj? – wRAR

+0

Tam mesajın tamamını güncelledim. :) Yardımınız için teşekkürler! – user805981

İlgili konular