【发布时间】:2016-01-28 02:13:00
【问题描述】:
任务是:阅读推文并根据特定时间(月-日-年-小时)将它们分成组。与特定时间相关的推文将存储在文件夹中的单独文件中。文件名为“Mon-Day-Year-Hour.txt”。
我是 python 新手,直到几天前才开始在它中编写代码。截至目前,我已将推文来自的文件加载到列表中,并根据创建时间对列表进行了排序。我研究了 itertools.groupby() 函数,但我不确定如何正确实现它或为了我的目的。
这是我目前所拥有的一些东西:
for line in open("CrimeReport.txt", "r").readlines():
tweet = json.loads(line)
tweets.append(tweet)
排序的推文:
sorted_tweets = sorted(tweets, key=lambda item:datetime.datetime.strptime(item['created_at'],
'%a %b %d %H:%M:%S +0000 %Y'))
我为糟糕的格式道歉。
【问题讨论】:
标签: python python-2.7 sorting timestamp tweets