I am loading a JSON file to parse it and convert it (only a part of the JSON) to a CSV.
So at the end of the method I would free the space of the loaded JSON.
Here is the method:
def JSONtoCSV(input,output):
outputWriter = csv.writer(open(output,'wb'), delimiter=',')
jsonfile = open(input).read()
data = loads(jsonfile)
for k,v in data["specialKey"].iteritems():
outputWriter.writerow([v[1],v[5]])
How do you free the space of the “data” variable?
should do it if you only have one reference. Keep in mind this will happen automatically when the current scope ends (the function returns).
Also, you don’t need to keep the
jsonfilestring around, you can justto read the JSON data directly from the file.
If you want
datato go away as soon as you’re done with it, you can combine all of that:since there is no reference to the data once the loop has ended, Python will free the memory immediately.