从文件加载“状态”对象?
Loading `status` objects from File?
几个月前,我开始从 Twitter 上抓取推文用于数据分析项目。我使用 Tweepy 和 python3.3 来获取状态对象并将它们转储到一个文件中,每天一个文件。
我这样做主要是因为我只想快速收集数据,但是我遇到了这个问题:
由于状态对象现在是字符串,我无法使用 tweepy - 据我所知 - 通过从我的文件加载它们来将它们转换回来。
这很糟糕,因为我现在意识到我真的只需要对象的 status._json
部分。不管出于什么原因,3 个月前我不这么认为。
我的问题是:
是否有已知的方法将这些 status
对象从字符串转换回?
我已经检查了 Tweepy
文档并进行了谷歌搜索,我很确定给定的工具无法做到这一点。
我能看到的唯一选择是手动拆分字符串,这看起来很丑陋。
保存在我的文件中的状态对象的示例:
pastebin
这些是按行存储的,每次从 Twitter 抓取新的时只需将它们附加到文件即可。
这不是您期望的答案,但可能会提供一个起点。
我拿了一个你的 Status
记录的实例,把它放在 text file 中,运行 这个脚本:
# coding: utf-8
with open('status.txt') as f:
tco = f.read()
import re
re.compile("(?P<key>\w+)=(?P<value>\w+)")
expre = re.compile("(?P<key>\w+)=(?P<value>\w+)")
pairs = dict(re.findall(expre, tco))
这给你这样的东西:
{'author': 'User',
'contributors': 'None',
'contributors_enabled': 'False',
'coordinates': 'None',
'created_at': 'datetime',
'default_profile': 'True',
'default_profile_image': 'False',
'favorite_count': '0',
'favorited': 'False',
'favourites_count': '46',
'follow_request_sent': 'None',
'followers_count': '204',
'following': 'False',
'friends_count': '274',
'geo': 'None',
'geo_enabled': 'True',
'id': '652242063048724480',
'in_reply_to_screen_name': 'None',
'in_reply_to_status_id': 'None',
'in_reply_to_status_id_str': 'None',
'in_reply_to_user_id': 'None',
'in_reply_to_user_id_str': 'None',
'is_quote_status': 'False',
'is_translator': 'False',
'listed_count': '91',
'location': 'None',
'notifications': 'None',
'place': 'None',
'possibly_sensitive': 'False',
'profile_background_tile': 'False',
'profile_use_background_image': 'True',
'protected': 'False',
'retweet_count': '0',
'retweeted': 'False',
'statuses_count': '9724',
'truncated': 'False',
'user': 'User',
'utc_offset': '7200',
'verified': 'False'}
很明显,这缺少了很多我的简单正则表达式无法解析的信息。 User
对象属性,例如。还有一些 json 听写。
对于您手头的问题中更复杂的事情,我建议您查看 parser
模块。不过,我会看看在空闲时间我能做些什么来解决这个问题。似乎是个好问题。
几个月前,我开始从 Twitter 上抓取推文用于数据分析项目。我使用 Tweepy 和 python3.3 来获取状态对象并将它们转储到一个文件中,每天一个文件。
我这样做主要是因为我只想快速收集数据,但是我遇到了这个问题:
由于状态对象现在是字符串,我无法使用 tweepy - 据我所知 - 通过从我的文件加载它们来将它们转换回来。
这很糟糕,因为我现在意识到我真的只需要对象的 status._json
部分。不管出于什么原因,3 个月前我不这么认为。
我的问题是:
是否有已知的方法将这些 status
对象从字符串转换回?
我已经检查了 Tweepy
文档并进行了谷歌搜索,我很确定给定的工具无法做到这一点。
我能看到的唯一选择是手动拆分字符串,这看起来很丑陋。
保存在我的文件中的状态对象的示例:
pastebin
这些是按行存储的,每次从 Twitter 抓取新的时只需将它们附加到文件即可。
这不是您期望的答案,但可能会提供一个起点。
我拿了一个你的 Status
记录的实例,把它放在 text file 中,运行 这个脚本:
# coding: utf-8
with open('status.txt') as f:
tco = f.read()
import re
re.compile("(?P<key>\w+)=(?P<value>\w+)")
expre = re.compile("(?P<key>\w+)=(?P<value>\w+)")
pairs = dict(re.findall(expre, tco))
这给你这样的东西:
{'author': 'User',
'contributors': 'None',
'contributors_enabled': 'False',
'coordinates': 'None',
'created_at': 'datetime',
'default_profile': 'True',
'default_profile_image': 'False',
'favorite_count': '0',
'favorited': 'False',
'favourites_count': '46',
'follow_request_sent': 'None',
'followers_count': '204',
'following': 'False',
'friends_count': '274',
'geo': 'None',
'geo_enabled': 'True',
'id': '652242063048724480',
'in_reply_to_screen_name': 'None',
'in_reply_to_status_id': 'None',
'in_reply_to_status_id_str': 'None',
'in_reply_to_user_id': 'None',
'in_reply_to_user_id_str': 'None',
'is_quote_status': 'False',
'is_translator': 'False',
'listed_count': '91',
'location': 'None',
'notifications': 'None',
'place': 'None',
'possibly_sensitive': 'False',
'profile_background_tile': 'False',
'profile_use_background_image': 'True',
'protected': 'False',
'retweet_count': '0',
'retweeted': 'False',
'statuses_count': '9724',
'truncated': 'False',
'user': 'User',
'utc_offset': '7200',
'verified': 'False'}
很明显,这缺少了很多我的简单正则表达式无法解析的信息。 User
对象属性,例如。还有一些 json 听写。
对于您手头的问题中更复杂的事情,我建议您查看 parser
模块。不过,我会看看在空闲时间我能做些什么来解决这个问题。似乎是个好问题。