从文本文件访问字段

2 投票
3 回答
527 浏览
提问于 2025-04-16 23:51

我刚开始学习Python。我需要从一个文本文件中获取一些信息,这个文件其实是以JSON格式存储的推文流。这个文本文件的内容大概是这样的:

{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'。

3 个回答

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
2

我很怀疑那是原始的JSON文本格式。在我看来,那看起来像是你用json.loads()这个方法在Python中加载后的结果。

既然它已经是一个字典了,你只需要用data['screen_name']来获取你想要的信息。

2

这看起来更像是一个Python字符串,而不是json格式。如果你已经有了一个字符串,比如说s,你可以用下面的方式把它转换成Python能直接使用的数据结构。

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)

撰写回答