我遇到了一个问题,涉及到路径变量的编码以及将其插入到SQLite数据库中。我尝试使用encode("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类型,但当path变量的类型是str时,仍然存在相同的错误。
sqlite3.ProgrammingError:除非您使用可以解释8位bytestrings(例如text_factory = str)的text_factory,否则不得使用8位bytestrings。强烈建议您改为使用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'编解码器无法解码位置32中的字节0xc5:序数不在范围(128)内
Python版本为2.7.2
unicode
了吗? - newtover