Python:UTF-8 问题(再次......)
我有一个数据库,每天两次与外部网络源同步。该网络源包含一堆条目,其中包含名称以及有关这些名称的一些额外信息。
其中一些名称很愚蠢,我想在将它们插入我自己的数据库时重命名它们。为了重命名这些愚蠢的名称,我有一个标准字典:
RENAME_TABLE = { "Wσird" : "Weird", ... }
如您所见,这就是 UTF-8 发挥作用的地方。这是对所有有问题的条目执行重命名的函数:
def rename_all_entries():
all_keys = RENAME_TABLE.keys()
entries = Entry.objects.filter(name__in=all_keys)
for entry in entries:
entry.name = RENAME_TABLE[entry.name]
entry.save()
因此它尝试在 RENAME_TABLE
中查找旧名称,并在找到时重命名该条目。但是,在使用 RENAME_TABLE[entry.name]
时出现 KeyError
异常。
现在我迷失了,我该怎么办?我...
# -*- coding: utf-8 -*-
...在Python 文件的顶部。
I have a database which is synchronized against an external web source twice a day. This web source contains a bunch of entries, which have names and some extra information about these names.
Some of these names are silly and I want to rename them when inserting them into my own database. To rename these silly names, I have a standard dictionary as such:
RENAME_TABLE = { "Wσird" : "Weird", ... }
As you can see, this is where UTF-8 comes into play. This is the function which performs renaming of all the problematic entries:
def rename_all_entries():
all_keys = RENAME_TABLE.keys()
entries = Entry.objects.filter(name__in=all_keys)
for entry in entries:
entry.name = RENAME_TABLE[entry.name]
entry.save()
So it tries to find the old name in RENAME_TABLE
and renames the entry if found. However, I get a KeyError
exception when using RENAME_TABLE[entry.name]
.
Now I'm lost, what do I do? I have...
# -*- coding: utf-8 -*-
...in the top of the Python file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到的错误是由于您希望字典中不存在 unicode 字符串。回想一下,在 Python 2.x 中(我假设您正在使用它),默认字符串类型是 8 位,而不是 unicode,因此您实际上是使用 8 位字符串对字典进行键控。要声明 unicode 字符串,请使用 u"my unicode string"。那么它应该被接受为密钥。
The error you are receiving is due to the unicode string you want not being in the dictionary. Recall that in Python 2.x (I assume you are using that), the default string type is 8-bit, not unicode, so you are actually keying the dictionary with 8-bit strings. To declare a unicode string, use u"my unicode string". Then it should be accepted as a key.