我在编码 路径变量并将其插入到 SQLite数据库时遇到了问题。我试图用 编码(“ utf-8”)函数解决这个问题,但是没有用。然后我使用 unicode()
函数,它给我类型 Unicode。
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类型,但是我仍然有相同的错误,当 路径变量的类型是 STR时出现了这个错误
编程错误: 不能使用8位字节字符串,除非 您可以使用一个 text _ Factory 来解释8位字节串(如 我们强烈建议您只使用 将应用程序切换为 Unicode 字符串。
你能帮我解决这个错误,并解释 encode("utf-8")
和 unicode()
函数的正确用法吗?我经常用它来战斗。
execute()
的声明提出了一个错误:
cur.execute("update docs set path = :fullFilePath where path = :path", locals())
我忘了更改 fullFilePath
变量的编码,这个变量也有同样的问题,但是我现在很困惑。我应该只使用 unicode()
还是 encode("utf-8")
,还是两者都使用?
我不能用
fullFilePath = unicode(fullFilePath.encode("utf-8"))
因为它提出了这个错误:
UnicodeDecodeError: ‘ ascii’编解码器在位置上无法解码字节0xc5 32: 序数不在范围内(128)
Python version is < em > 2.7.2