0

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.

jimakos17
  • 905
  • 4
  • 14
  • 33
  • Take a look at https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python –  May 31 '16 at 14:03
  • 1
    Maybe you should consider replacing that csv file with an actual database. – Ulisha May 31 '16 at 14:21

1 Answers1

1

I would suggest to use another protocol to get your data shuffled from your producer to your consumer, for example a socket or a pipe. The approach pointed to by Lutz also works of course, but it relies on using a tool provided by the OS (tail).

nucleon
  • 1,128
  • 1
  • 6
  • 19