在Python中使用unicode()和encode()函数 [英] Usage of unicode() and encode() functions in Python

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

问题描述

我对路径变量的编码有问题,并将其插入到SQLite 数据库中。我试图用编码(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 类型,但是我仍然有相同的错误当路径变量的类型为 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位bytestrings的text_factory(如
text_factory = str),否则不能使用8位的除错测试。强烈建议您只需
将您的应用程序切换为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()语句引发错误:

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'编解码器无法解码位置
32的字节0xc5:序号不在范围(128)

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

Python 版本是2.7.2

Python version is 2.7.2

推荐答案

您正在使用 encode(utf-8)不正确。 Python字节串( str 类型)具有编码,Unicode不。您可以使用 uni.encode(encoding)将Unicode字符串转换为Python字符串,您可以使用将字节字符串转换为Unicode字符串s.decode(encoding)(或者等价于 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)).

如果 fullFilePath 路径目前是一个 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()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆