In Python 2.7 CrimeReport is Available at https://pastebin.com/UZVftJYH » Task 7
ID: 3879707 • Letter: I
Question
In Python 2.7
CrimeReport is Available at https://pastebin.com/UZVftJYH
» Task 7: Data Preprocessing Read the tweets from the file "CrimeReport.txt" and print the id for each tweet. Here are some functions that you will use in the task: open().readlines(), tweet - json.loads(), print tweet.keys, you will know the keys of tweet dictionary object, then you can find which key relates to tweet id, and you can then retrieve the id of this specific tweet. » Task 8: Data Preprocessing: tweets filtering INPUT: "CrimeReport.txt" OUTPUT: a file "task8.data" that stores the 10 most recent tweets Suggestions tweet created- at' gives the created time of this tweet. Rank tweets based on the time from the earliest to the most recent. Then we can identify the 10 most recent tweets. Some example lines that are not directly runnable import datetime tweets for line in open (). readlines ) tweet json. loads ( line) tweets.append (tweet) #datetime. datetime. strptime (item , created-at j , %a %b %d 3H:%M%S +0000 %Y') #converts the string format of a date time to the datetime object lambda item: , created-at ,] , sorted-tweets = sorted (tweets, key datetime. datetime. strptime (item ,%a %b %d 3H:XM:%S +0000 %Y')) # sorted tweets based on time. f = open ( ' output . txt ' , 'w' ) for tweet in sorted-tweets-5: ]: f. write (json. dumps(tweet) +'') f. close () Note, when you copy and paste the code above, please be careful with the proper indentation and quotation mark.Explanation / Answer
Task7
----------------------------------------------------------------------
""" Read the tweets from the file "CrimeReport.txt" and print the
id for each tweet.. """
import json;
def main():
for line in open('CrimeReport.txt').readlines():
tweet = json.loads(line)
if tweet['id']:
print (tweet['id'])
if __name__ == '__main__':
main()
-------------------------------------------------------------------------------------------
task8
--------------------------------------------------
INPUT: "CrimeReport.txt"
OUTPUT: a file "output.txt" that stores the 10 most recent tweets """
import datetime
import json
def main():
tweets = []
for line in open("CrimeReport.txt").readlines():
tweet=json.loads(line)
if tweet['created_at']:
print tweet
tweets.append(tweet)
datetime.datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
sorted_tweets = sorted(tweets, key = lambda tweet: datetime.datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y'))
f = open('output.txt', 'w')
for tweet in sorted_tweets[-10:]:
f.write(json.dumps(tweet) + ' ')
f.close()
if __name__ == '__main__':
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.