2013-02-25 33 views
6

mới để trăn .... Đang cố gắng để có được những phân tích cú pháp để giải mã đúng vào một cơ sở dữ liệu SQLite nhưng nó sẽ không hoạt động :(Lỗi Loại: giải mã Unicode không được hỗ trợ

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

tôi tiếp tục nhận được sau lỗi: 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 

tôi có thể làm điều gì đó thats thực sự ngu ngốc ... :(Hãy cho tôi biết những gì tôi đang làm sai ... Cảm ơn

+1

Ahhhh, unicode. Lệnh cấm của mọi python phát sinh, tại sao bạn lại sử dụng 'unicode()'? sử dụng 'u '博 人" 'và thực hiện theo cách pythonic. – Amelia

+0

@Hiroto bạn đang đề xuất thay thế mã bằng chữ? – wRAR

+0

@wRAR Tôi có nghĩa là nhận xét ra các bộ phận để chèn. Các cuộc gọi 'unicode()' trong chính mã đó là dự phòng và vứt lỗi anyway – Amelia

Trả lời

6

!đã là unicode, như trường cols[0].string chứa unicode (giống như documented).

+0

okay .... vì vậy tôi đã sử dụng x thay vì xx ngay bây giờ ... Tôi nhận được một 'Traceback (cuộc gọi gần đây nhất): Tệp" sqlitetestbed.py ", dòng 52, trong main() Tệp "sqlitetestbed.py", dòng 48, trong chính insertChar (x, y, z) Tệp "sqlitetestbed.py", dòng 20, trong insertChar VALUES (?,?,?) "" ", (Xếp hạng, word, definition)) 'Tại sao vậy? – user805981

+0

@ user805981 là toàn bộ tin nhắn? – wRAR

+0

Tôi vừa cập nhật tin nhắn đầy đủ. :) Cảm ơn sự giúp đỡ của bạn! – user805981

Các vấn đề liên quan