Accessing values from a Unicode dictionary in Python
Writing by shivdev on Saturday, 8 of November , 2014 at 10:05 pm
I was running into several errors while accessing a dictionary persisted in MongoDB as JSON (with Unicode) until I found this post on stackoverflow.com.
TypeError: string indices must be integers, not unicode
So basically the way to workaround the problem is as follows:
import json, ast
# You would assume this would work ...
somekey_val = dict[SOME_KEY]
# but due to frustrations with unicode in python handle it
try:
somekey_val = ast.literal_eval(dict[SOME_KEY])
except Exception, e:
print 'unicode conversion not needed for some_key'
# do regular processing from here on
process( somekey_val )
Leave a comment
Category: Python
- Add this post to
- Del.icio.us -
- Digg -
- -
- Tweet -
-
-
No comments yet.
You must be logged in to post a comment.