Python 中 unicode() 和 encode() 函数的使用

Usage of unicode() and encode() functions in Python(Python 中 unicode() 和 encode() 函数的使用)

本文介绍了Python 中 unicode() 和 encode() 函数的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对 path 变量进行编码并将其插入到 SQLite 数据库时遇到问题.我试图用 encode("utf-8") 函数解决它,但没有帮助.然后我使用了 unicode() 函数,它给了我类型 unicode.

I have a problem with encoding of the path variable and inserting it to the SQLite database. I tried to solve it with encode("utf-8") function which didn't help. Then I used unicode() function which gives me type 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

Finally I gained unicode type, but I still have the same error which was present when the type of the path variable was str

sqlite3.ProgrammingError: 你不能使用 8 位字节串,除非您使用可以解释 8 位字节串的 text_factory(例如text_factory = str).强烈建议您改为将您的应用程序切换到 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()函数的正确用法吗?我经常与它作斗争.

Could you help me solve this error and explain the correct usage of encode("utf-8") and unicode() functions? I'm often fighting with it.

这个execute() 语句引发了错误:

This execute() statement raised the error:

cur.execute("update docs set path = :fullFilePath where path = :path", locals())

我忘记更改遇到相同问题的 fullFilePath 变量的编码,但我现在很困惑.我应该只使用 unicode()encode("utf-8") 还是两者都使用?

I forgot to change the encoding of fullFilePath variable which suffers with the same problem, but I'm quite confused now. Should I use only unicode() or encode("utf-8") or both?

我不能用

fullFilePath = unicode(fullFilePath.encode("utf-8"))

因为它引发了这个错误:

because it raises this error:

UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xc5 的位置32:序号不在范围内(128)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 32: ordinal not in range(128)

Python 版本是 2.7.2

推荐答案

您正在错误地使用 encode("utf-8").Python 字节串(str 类型)有编码,Unicode 没有.您可以使用 uni.encode(encoding) 将 Unicode 字符串转换为 Python 字节字符串,也可以使用 s.decode(encoding) 将字节字符串转换为 Unicode 字符串>(或等效地,unicode(s, encoding)).

You are using encode("utf-8") incorrectly. Python byte strings (str type) have an encoding, Unicode does not. You can convert a Unicode string to a Python byte string using uni.encode(encoding), and you can convert a byte string to a Unicode string using s.decode(encoding) (or equivalently, unicode(s, encoding)).

如果 fullFilePathpath 当前是 str 类型,您应该弄清楚它们是如何编码的.例如,如果当前编码是 utf-8,您将使用:

If fullFilePath and path are currently a str type, you should figure out how they are encoded. For example, if the current encoding is utf-8, you would use:

path = path.decode('utf-8')
fullFilePath = fullFilePath.decode('utf-8')

如果这不能解决问题,实际问题可能是您没有在 execute() 调用中使用 Unicode 字符串,请尝试将其更改为以下内容:

If this doesn't fix it, the actual issue may be that you are not using a Unicode string in your execute() call, try changing it to the following:

cur.execute(u"update docs set path = :fullFilePath where path = :path", locals())

这篇关于Python 中 unicode() 和 encode() 函数的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Python 中 unicode() 和 encode() 函数的使用

基础教程推荐