Repository navigation
Expand file tree
/
Copy pathfindhashtags.py
More file actions
52 lines (42 loc) · 1.37 KB
/
Copy pathfindhashtags.py
File metadata and controls
52 lines (42 loc) · 1.37 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import pandas as pd
import matplotlib.pyplot as plt
import re
# test
def word_in_text(word, text):
text = str(text)
word = word.lower()
text = text.lower()
match = re.search(word, text)
if match:
return True
return False
tweets_data_path = 'Output.txt'
tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
print (len(tweets_data))
tweets = pd.DataFrame()
for line in tweets_data:
try:
# Read in one line of the file, convert it into a json object
#tweet = json.loads(line.strip())
tweet = line
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:
continue