We are currently running a program which is already taken an hour to execute (and still not finished), so we are wondering if we can improve our program code so it will run faster.
Our program consists of two parts: first we need to change strings, because we use JSON dictionaries and the data has similar keys ("track") for all items -- if we don't do this, the output gives only the first track. Second, we need to print the JSON data to a csv file.
Snippet of the JSON file: (actual file is around 900 mb)
{
"VV":{
"Version":1,
"Data":[
{
"track":[
{
"time":"YYYY-MM-DDTHH:MM:SS:MS",
"tl":{
"x":10,
"y":11
},
"br":{
"x":20,
"y":20
}
},
{
"time":"YYYY-MM-DDTHH:MM:SS:MS",
"tl":{
"x":12,
"y":15
},
"br":{
"x":22,
"y":23
}
}
],
"track":[
{
"time":"YYYY-MM-DDTHH:MM:SS:MS",
"tl":{
"x":30,
"y":39
},
"br":{
"x":40,
"y":45
}
},
{
"time":"YYYY-MM-DDTHH:MM:SS:MS",
"tl":{
"x":12,
"y":18
},
"br":{
"x":22,
"y":24
}
}
]
}
]
}
}
First part of our code:
with open(r'filename.json') as json_file:
fil = json_file.read()
i = 0
print i
while ('track' in fil) :
fil = fil.replace('track', 'tr'+str(i), 1)
i = i + 1
print i
input_data = json.loads(fil)
data_d = input_data['VV']['Data'][0]
Second part:
with open(r'output.csv', 'wb') as csv_file:
writer = csv.writer(csv_file)
i = 0
for track, data in data_d.items():
i = i+1 # Track
for item in data:
#TRACK
item_values = []
item_values.append(i)
#DAY
#print item['time']
day = item['time'][8:10]
item_values.append(day)
#COORDINATEN
item_values.append(item['tl']['x'])
item_values.append(item['tl']['y'])
item_values.append(item['br']['x'])
item_values.append(item['br']['y'])
#TIME
time = item['time'][11:13]+item['time'][14:16]+item['time'][17:19]+item['time'][20:23]
item_values.append(time)
writer.writerow(item_values)