Load and explore your twitter data
Now that you've got your Twitter data sitting locally in a text file, it's time to explore it! This is what you'll do in the next few interactive exercises. In this exercise, you'll read the Twitter data into a list tweets_data.
This exercise is part of the course
Importing Data in Python
Exercise instructions
- Assign the filename
'tweets.txt'to the variabletweets_data_path. - Initialize
tweets_dataas an empty list to store the tweets in. - Within the for loop initiated by
for line in tweets_file:, load each tweet into a variabletweetusingjson.loads(), then appendtweettotweets_datausing theappend()method. - Hit submit and check out the keys of the first tweet dictionary printed to the shell.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import package
import json
# String of path to file: tweets_data_path
# Initialize empty list to store tweets: tweets_data
# Open connection to file
tweets_file = open(tweets_data_path, "r")
# Read in tweets and store in list: tweets_data
for line in tweets_file:
____
____
# Close connection to file
tweets_file.close()
# Print the keys of the first tweet dict
print(tweets_data[0].keys())