-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_JSON.py
30 lines (25 loc) · 1.12 KB
/
tweet_JSON.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Import the necessary package to process data in JSON format
try:
import json
except ImportError:
import simplejson as json
tweets_filename = 'twitter_stream_tweet10.txt'
tweets_file = open(tweets_filename, "r")
for line in tweets_file:
try:
# Read in one line of the file, convert it into a json object
tweet = json.loads(line.strip())
if 'text' in tweet: # only messages contains 'text' field is a tweet
print tweet['id'] # This is the tweet's id
print tweet['created_at'] # when the tweet posted
print tweet['text'] # content of the tweet
print tweet['user']['id'] # id of the user who posted the tweet
print tweet['user']['name'] # name of the user, e.g. "Wei Xu"
print tweet['user']['screen_name'] # name of the user account, e.g. "cocoweixu"
hashtags = []
for hashtag in tweet['entities']['hashtags']:
hashtags.append(hashtag['text'])
print hashtags
except:
# read in a line is not in JSON format (sometimes error occured)
continue