views:

108

answers:

1

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.

+4  A: 

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.

abc
Oh, darn, thanks a lot! :)
Deniz Dogan