Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with unicode keys in a python dictionary

I am learning about the Twitter API using Python 2.7.x. I've saved a number of random tweets and I am trying to process them. Each tweet is converted to a dictionary with json.loads and all the dictionaries are part of a list.

Given a single tweet, I want to be able to extract certain fields from the dictionary. The keys are all unicode strings. If I iterate through the keys in a loop, I have no trouble printing the values:

for i in tweet.keys():
    print i, tweet[i]

So the loop above works fine, but I have had no luck figuring out how to manually specify key. "u'text'" is the key for the actual tweet content (the user's actual post). If I try to print tweet['text'], I get a KeyError. I naively tried tweet[u'text'] but that fails with a KeyError too.

I guess I am curious about the difference between what the loop is doing as it steps through tweet.keys() vs. what I am doing when manually I specifying a key. Note that if I print the value of i in the loop above, the key name is printed, but without the unicode wrapping. When the key is "u'text'", the value of i is just 'text', or at least that is what is printed to the terminal.

like image 378
drumboots Avatar asked Jul 02 '14 13:07

drumboots


1 Answers

Python 2 handles translation between str and unicode keys transparently for you, provided the text can be encoded to ASCII:

>>> d = {u'text': u'Foo'}
>>> d.keys()
[u'text']
>>> 'text' in d
True
>>> u'text' in d
True
>>> d['text']
u'Foo'
>>> d[u'text']
u'Foo'

This means that if you get a KeyError for tweet['text'], then that dictionary has no such key.

like image 68
Martijn Pieters Avatar answered Nov 10 '22 22:11

Martijn Pieters