I have a csv file in which I append new results every 10 seconds. Every so (specified from a user) I read this csv and make a JSON file. I don't want to convert the whole csv into a JSON every time but only the last part that has been updated. So, I imagine that I will keep the last line every time, then I will start reading the file from this line and then to convert it to a JSON format.
myJson = {}
try:
with open('myFile.csv', 'r') as f: #Read the csv file with read privileges.
for line in f:
lst = re.split(r' +', line.rstrip('\t')) #Columns are seperated with tabs.
if len(lst) == 3: #There are three columns in the file:
n = lst[0].strip() #Names
v = lst[1].strip() #Values
p = lst[2].strip() #Percentages
try:
myJson[n].append(v) #Add values to the according keys.
except KeyError:
myJson[n] = [v] #Handle potential KeyErrors.
except IOError:
print "csv file has not been created yet."
The simplest way is to delete the csv file each time but it would be much more useful for me to keep it and just create new JSON files.