forked from DylanDrein/Python-FYP
/
CSVScript.py
44 lines (31 loc) · 1.03 KB
/
CSVScript.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
import csv
import os
#dictionary to keep track of files we have written to so far
fileHandles={}
count=0
lastName=None
handle=None
filenum = 0
rootdir = "./UsersReversed/"
for current_directory, directories, files in os.walk(rootdir):
for file in files:
filenum += 1
filepath = os.path.join(current_directory,file)
with open(filepath) as infile:
for line in infile:
count+=1
parsed=json.loads(line)
name=parsed['name']
#only change filehandle if lastName is different to this name
if(lastName!=name):
if name in fileHandles:
handle=fileHandles[name]
else:
#this is the first time we are seeing this name. If a corresponding file already exists, delete it.
filename= "./CSVSeparated/" + name.lower().replace(" ","-")+".csv"
handle=fileHandles[name]=open(filename, 'wb')
del parsed['cX']
del parsed['cY']
c = csv.writer(handle, delimiter = ',')
c.writerow([parsed['name'], parsed['time'], parsed['y'], parsed['x'], parsed['type']])