gpt4 book ai didi

python - 类型错误 : decoding Unicode is not supported

转载 作者:太空狗 更新时间:2023-10-29 23:59:48 25 4
gpt4 key购买 nike

python 新手....试图让解析器正确解码到 sqlite 数据库中,但它不会工作:(

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

我不断收到以下错误:TypeError:不支持解码 Unicode

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

我可能正在做一些非常愚蠢的事情...:( 请告诉我我做错了什么...谢谢!

最佳答案

x 已经是 unicode,因为 cols[0].string 字段包含 unicode(就像documented ).

关于python - 类型错误 : decoding Unicode is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com