Sorting file into Groups Python -
the task to: read tweets , separate them groups based on specific hours (month-day-year-hour). tweets related specific hour stored in separate file in folder. file name "mon-day-year-hour.txt".
i new python, starting coding in of couple of days ago class. of right have file tweets came loaded list, , have sorted list based on time created. have looked itertools.groupby() function, i'm not sure how implement correctly or purpose.
here's bit of have far:
for line in open("crimereport.txt", "r").readlines(): tweet = json.loads(line) tweets.append(tweet) sorted tweets:
sorted_tweets = sorted(tweets, key=lambda item:datetime.datetime.strptime(item['created_at'], '%a %b %d %h:%m:%s +0000 %y')) i apologize poor formatting.
dic = {} key, value in groupby(v, lambda x: x%2): if key not in dic.keys(): dic[key] = list(value) else: dic[key] += list(value) groupby data, , function returns each data's id. iterating on groupby , adding data dictionary have grouped. if have large data, dictionary may not fast enough.
Comments
Post a Comment