Working with unicode keys in a python dictionary

2019-01-27 13:17发布

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.

2条回答
虎瘦雄心在
2楼-- · 2019-01-27 14:01

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.

查看更多
放我归山
3楼-- · 2019-01-27 14:01
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {u'text': u'Foo'}
>>> print "d:{text}".format(**d)
d:Foo
查看更多
登录 后发表回答