在 Python 中附加数据后重新打开 CSV 的问题

发布于 2025-01-19 14:14:30 字数 1379 浏览 5 评论 0原文

我有这个工作按钮并且它确实保存了,但我的问题是我可以从同一个文件再次启动我的 tkinter 程序。它有效,如果需要更多可以共享它,但循环是从 tkinter 中我的列的 csv 中分割出来的。一旦向其中添加数据,我的 python 脚本就无法再次读取它。

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])

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])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2025-01-26 14:14:30

for循环比您需要的更复杂,请尝试以下操作:

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

无需list(range(0,len(data)))

也,请勿使用Global 文件 >。

创建一种读取数据的方法,而另一种保存数据的方法,并根据需要打开文件,例如:

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

    return data 

使用使用您确定不使用时资源(文件)是关闭的最后

,尝试不将大写速度用于变量实例 htttps> htttps> htttps:// peps。 python.org/pep-0008/#naming-conventions

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文