-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterJSON.py
More file actions
31 lines (29 loc) · 1.29 KB
/
Copy pathfilterJSON.py
File metadata and controls
31 lines (29 loc) · 1.29 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
import json
#with open('C:\Users\SigurdLap\Desktop\Master\geotweets3_first_10.txt', 'r') as f:
myFile = open('exampleFilter1.txt', 'a')
with open('D:\Twitter\geotweets1.txt', 'r') as f:
try:
for _ in xrange(3):
next(f)
for line in f: # read only the first tweet/line
try: # handle JSON errors
tweet = json.loads(line) # load it as Python dict
myFile.write(json.dumps(tweet['place']['country_code']) + ', ')
myFile.write(json.dumps(tweet['created_at']) + ', ')
myFile.write(json.dumps(tweet['source']) + ', ')
myFile.write(json.dumps(tweet['id']) + ', ')
# myFile.write(json.dumps(tweet['user']['lang']) + ', ')
myFile.write(json.dumps(tweet['lang']) + ', ')
# myFile.write(json.dumps(tweet['user']['location']) + ', ')
myFile.write(json.dumps(tweet['user']['time_zone']) + ', ')
myFile.write(json.dumps(tweet['user']['lang']) + ', ')
myFile.write('\n')
except ValueError:
pass
except RuntimeError:
pass
except TypeError:
pass
except Exception:
print "Unexpected error:", sys.exc_info()[0]
raise