Python: UTF-8 problems (again...)
Posted
by blahblah
on Stack Overflow
See other posts from Stack Overflow
or by blahblah
Published on 2010-03-28T21:16:46Z
Indexed on
2010/03/28
21:23 UTC
Read the original article
Hit count: 149
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 = { "Wsird" : "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.
© Stack Overflow or respective owner