0

I have this working button and it does save, but my issue is I can start my tkinter program again from the same file. It works and if need more can share it, but the loop is where split from the csv for my columns in tkinter. Just can't get my python script to read it again once add data to it.

filePath = '..\glucose_values.csv'

    #Variables
    File = open(filePath)
    f =open(filePath)
    Reader = csv.reader(File)
    newline='\n'
    Data= list(Reader)
    main_lst=[]
    lst=[]

  
#Store Button Function
    def store():
        with open('..\glucose_values.csv', "a", encoding='utf-8') as f:
            Writer= csv.writer(f)
            Writer.writerow(main_lst)
            messagebox.showinfo("Information","Saved succesfully")
           # df= pd.DataFrame(Data)
           # df.to_csv(f)
        return None
    
    
    
    def Add():
       #working
       lst=[timeEnter.get()]
       lst2=[dateEnter.get()]
       lst3=[bloodEnter.get()]
       main_lst.extend(lst+ lst2+lst3)
       print(lst)
       messagebox.showinfo("Information","The data has been added successfully")
       print(main_lst)
       return None
    
    def saveMe ():
        Add()
        global File
        #File =
        return None
    
    #GRab column informatin
    list_of_dates = []
    list_of_times =[]
    list_of_blood=[]
    #list_of_
    for x in list(range(0, len(Data))):
        list_of_dates.append(Data[x][0])
        list_of_times.append(Data[x][1])
        list_of_blood.append(Data[x][2])
alex b
  • 3
  • 1
  • Did you compare the csv file before and after you saved it? Why you open the file in append mode to save the csv in the sotre() method? If main_lst have all the data you should just overwrite the file. – Gonzalo Odiard Apr 07 '22 at 00:11
  • I was going to collect everything in one function and then have one add a line to an existing csv file. So if I take what have and just have it combined and add it back to the csv file? – alex b Apr 09 '22 at 02:33
  • I think my issue is the for loop at the bottom of it when I append the new line. I noticed if the line get cleared it will allow me to rerun the program without the below error, so something is either wrong in the store or the loop. Just stuck on it error: list_of_dates.append(Data[x][0]) IndexError: list index out of range – alex b Apr 09 '22 at 18:22

1 Answers1

0

The for loop is more complex that you need, try something like:

>>> l = ['a', 'b', 'c']
>>> for i in range(len(l)):
...     print(l[i])
... 
a
b
c

no need for list(range(0, len(Data)))

also, do not use a global File.

Create a method to read the data, and another to save the data, and open your file as needed, like:

def read_data(filePath):
    with open(filePath) as myCsvFile:
        reader = csv.reader(myCsvFile)
        data = list(reader)

    return data 

using with you are sure that the resource (the file) is closed when you do not use it anymore

Finally, try to not use uppercase for instances of variables https://peps.python.org/pep-0008/#naming-conventions

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Gonzalo Odiard
  • 1,238
  • 12
  • 19