# coding=utf-8
import linecache
import time
now = time.time()
data_keys = ('bid', 'uid', 'username', 'v_class', 'content', 'img', 'created_at', 'source', 'rt_num', 'cm_num',
'rt_uid', 'rt_username', 'rt_v_class', 'rt_content', 'rt_img', 'src_rt_num', 'src_cm_num', 'gender',
'rt_bid', 'location', 'rt_mid', 'mid', 'lat', 'lon', 'lbs_type', 'lbs_title', 'poiid', 'links', 'hashtags',
'ats', 'rt_links', 'rt_hashtags', 'rt_ats', 'v_url', 'rt_v_url')
keys = {data_keys[i]: i for i in xrange(0, len(data_keys))}
F = linecache.getlines('twitter.txt')
lines = [x[1:-2].split('","') for x in F]
users = set(line[keys['username']] for line in lines)
users_total = len(users) # 用户总数
assert type(users_total) == int
users = list(users) # 用户名组成的list
assert type(users) == list
tweets_from_2012_11 = filter(lambda line: line[keys['created_at']].startswith('2012-11'), lines)
tweets_2012_11_count = len(tweets_from_2012_11) # 2012-11月的tweets的总数
assert type(tweets_2012_11_count) == int
dates = set(line[keys['created_at']].sp
分析python处理基本数据<四>
这篇博客展示了如何使用Python的linecache模块处理文本数据,分析了2012年11月的推特数据,包括用户总数、特定日期的推文数量、最活跃的小时、每天发推最多的用户、特定日期每个小时的推文频率、来源最多的推文、转发URL最多的用户、内容长度最长的用户、转发URL最多的用户、11点发推最多的用户以及源微博URL次数最多的用户。
摘要由CSDN通过智能技术生成