我有一个路径变量的编码问题,并将其插入到SQLite数据库中。我试图用编码(“utf-8”)函数解决它,但没有帮助。然后我使用unicode()函数,它给我类型unicode。在Python中使用unicode()和encode()函数
print type(path) # <type 'unicode'>
path = path.replace("one", "two") # <type 'str'>
path = path.encode("utf-8") # <type 'str'> strange
path = unicode(path) # <type 'unicode'>
最后我获得了的unicode类型,但我仍然有其存在时路径变量的类型是海峡同样的错误
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
你能帮我解决这个错误并解释encode("utf-8")
和unicode()
函数的正确用法?我经常与之战斗。
编辑:
这的execute()陈述引发的错误:
cur.execute("update docs set path = :fullFilePath where path = :path", locals())
我忘了改fullFilePath变量,同样的问题受到影响的编码,但我现在很困惑。我应该只使用unicode()或编码(“utf-8”)或两者吗?
我不能使用
fullFilePath = unicode(fullFilePath.encode("utf-8"))
,因为它提出了这样的错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 32: ordinal not in range(128)
的Python版本是2.7.2
哪里是引发错误的代码? – newtover 2012-04-23 20:48:04
您确切的问题已经得到解答:[http://stackoverflow.com/questions/2392732/sqlite-python-unicode-and-non-utf-data][1] [1]:http:// stackoverflow.com/questions/2392732/sqlite-python-unicode-and-non-utf-data – garnertb 2012-04-23 20:51:25
@newtover我编辑了这个问题。 – xralf 2012-04-23 20:55:58