2011-08-18 87 views
2

我是python的新手。我需要從一個基本上是json格式的tweet流的文本文件中訪問字段。該文本文件如下所示:從文本文件訪問字段

{u'favorited': False, u'entities': {u'user_mentions': [{u'indices': [76, 84], u'id': 10228272, u'id_str': u'10228272', u'name': u'YouTube', u'screen_name': u'YouTube'}], u'hashtags': [], u'urls': [{u'indices': [52, 71], u'url': u'http://t.co/iQYW4d3', u'expanded_url': u'http://www.youtube.com/watch?v=-HGfFyqJMrk', u'display_url': u'youtube.com/watch?v=-HGfFy\u2026'}]}, u'contributors': None, u'truncated': False, u'text': u'Long Live Egypt.....A MUST watch..... Freeeeedom... http://t.co/iQYW4d3 via @youtube', u'created_at': u'Sun Feb 06 17:18:21 +0000 2011', u'retweeted': False, u'in_reply_to_status_id_str': None, u'coordinates': None, u'id': 34299873733902336L, u'source': u'<a href="http://twitter.com/tweetbutton" rel="nofollow">Tweet Button</a>', u'in_reply_to_status_id': None, u'id_str': u'34299873733902336', u'in_reply_to_screen_name': None, u'user': {u'follow_request_sent': None, u'profile_use_background_image': True, u'id': 191652149, u'verified': False, u'profile_sidebar_fill_color': u'c9c9c9', u'profile_text_color': u'1c1f23', u'followers_count': 43, u'protected': False, u'location': u'Damascus - Syria', u'profile_background_color': u'07090b', u'listed_count': 3, u'utc_offset': 7200, u'statuses_count': 113, u'description': u'In heaven, all the interesting people are missing ', u'friends_count': 149, u'profile_link_color': u'c34242', u'profile_image_url': u'http://a3.twimg.com/profile_images/1125299662/Untitled_normal.jpg', u'notifications': None, u'show_all_inline_media': False, u'geo_enabled': False, u'id_str': u'191652149', u'profile_background_image_url': u'http://a0.twimg.com/profile_background_images/150071579/x07823fa2328f1ff92c4d900c44bc34d.jpg', u'screen_name': u'NourZoukar', u'lang': u'en', u'following': None, u'profile_background_tile': True, u'favourites_count': 0, u'name': u'M.Nour Zoukar', u'url': u'http://www.kawngroup.com', u'created_at': u'Fri Sep 17 00:19:26 +0000 2010', u'contributors_enabled': False, u'time_zone': u'Jerusalem', u'profile_sidebar_border_color': u'bfbfbf', u'is_translator': False}, u'place': None, u'retweet_count': 0, u'geo': None, u'in_reply_to_user_id_str': None, u'in_reply_to_user_id': None} 

我希望我的輸出顯示在這種情況下'NourZoukar'的屏幕名稱。

回答

2

我非常懷疑這是原始的JSON文本格式。對於我來說,看起來就像輸出到json.loads()的Python一樣。

鑑於它已經是一本字典,你只需要做data['screen_name']

+0

其實它看起來像它應該是'鳴叫[「用戶」] [「SCREEN_NAME」]',但肯定的。 – Johnsyweb

+0

我從文本文件複製了上面給出的文本,當我嘗試使用json.loads()加載文件時,它給出TypeError:期望的字符串或緩衝區。 – Annie

+1

你需要顯示你的實際代碼。你如何準確加載它? –

2

這看起來更像是一個Python字符串而不是json。如果您已經有一個字符串,說s你可以將其轉換成數據本地數據結構與

import ast 
d = ast.literal_eval(s) 

要讀取的字符串從stream.txt,使用類似

import ast, pprint 

with open('stream.txt') as fp: 
    stream = fp.read() 
    data = ast.literal_eval(stream) 

pprint.pprint(data) 
1

由於兩個@Daniel@hop表示,它看起來像tweetstream.txt包含JSON對象的Python表示而不是實際的JSON。

您可以將它讀回到Python中,並且每一行都將是一個字典,代表一條推文,包含代表用戶的另一個字典。下面是在Python 2.6的例子(版本是很重要的位置):

>>> import ast 
>>> with open('tweetstream.txt') as stream: 
...  line = stream.read() 
...  tweet = ast.literal_eval(line) 
...  print tweet['user']['screen_name'] 
... 
NourZoukar