开发者

Python: UTF-8 problems (again...)

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[en开发者_如何学Gotry.name].

Now I'm lost, what do I do? I have...

# -*- coding: utf-8 -*-

...in the top of the Python file.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜