在 Python 中使用 TKinter 保存并向 csv 文件添加新行

发布于 2025-01-14 15:01:24 字数 1099 浏览 5 评论 0原文

因此,我包含了一些无法从 TKinter 中的输入字段捕获数据并让它们覆盖 csv 文件的代码。它将覆盖 csv 文件,但不会像想要的那样在文件末尾添加新行。

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

#Store Button Function
def store():
    with open('./glucose_values.csv', "w+", encoding='utf-8') as f:
        Writer=csv.writer(File)
        csv.writer(["Date","Time","Glucose"])
        csv.writer(newline + main_lst)
        messagebox.showinfo("Information","Saved succesfully")
                
    return None

def Add():
   lst=[timeEnter.get(),dateEnter.get(),bloodEnter.get()]
   main_lst.append(lst)
   messagebox.showinfo("Information","The data has been added successfully")
   return None

#Buttons 
storeButton = Button(root, text="Store", command=store)
storeButton.grid(row=5, column=3 )

addButton = Button(root, text="Add", command=Add)
addButton.grid(row=5, column=1 )

#Enter data area
dateEnter= Entry(root, width = 10).grid(row=13, column=2)
timeEnter= Entry(root,  width = 10).grid(row=14, column=2)
bloodEnter= Entry(root,  width = 10).grid(row=15, column=2)

SO I've included some of my code having trouble capturing data from the entry fields in TKinter and having them overwrite a csv file. It will overwrite the csv file, but not add a new line at the end of the file like want.

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

#Store Button Function
def store():
    with open('./glucose_values.csv', "w+", encoding='utf-8') as f:
        Writer=csv.writer(File)
        csv.writer(["Date","Time","Glucose"])
        csv.writer(newline + main_lst)
        messagebox.showinfo("Information","Saved succesfully")
                
    return None

def Add():
   lst=[timeEnter.get(),dateEnter.get(),bloodEnter.get()]
   main_lst.append(lst)
   messagebox.showinfo("Information","The data has been added successfully")
   return None

#Buttons 
storeButton = Button(root, text="Store", command=store)
storeButton.grid(row=5, column=3 )

addButton = Button(root, text="Add", command=Add)
addButton.grid(row=5, column=1 )

#Enter data area
dateEnter= Entry(root, width = 10).grid(row=13, column=2)
timeEnter= Entry(root,  width = 10).grid(row=14, column=2)
bloodEnter= Entry(root,  width = 10).grid(row=15, column=2)

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

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

发布评论

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

评论(1

善良天后 2025-01-21 15:01:24

w+ 打开文件以进行写入和读取。 如果文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件以供读写。

因此,请改用 a ,因为使用 a 参数打开文件可让您追加到文件末尾,而不是简单地覆盖现有内容。

 with open('./glucose_values.csv', "a", encoding='utf-8') as f:
        Writer=csv.writer(File)

编辑:

csv.writer() 函数接受 csvfile 作为参数,并且您正在传递列表:

csv.writer(["日期","时间","血糖"])

csv.writer(换行符 + main_lst)


因此,正确的方法是使用 writerow() 方法:

def store():
    with open('./glucose_values.csv', "a+", newline='', encoding='utf-8') as f:
        Writer=csv.writer(f)
        Writer.writerow(["Date","Time","Glucose"])
        Writer.writerow(main_lst)

另外,为了追加到 CSV 文件中,建议使用 DictWriter() 方法。 参考此处

w+ opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Therefore, use a instead , as opening a file with the a parameter allows you to append to the end of the file instead of simply overwriting the existing content.

 with open('./glucose_values.csv', "a", encoding='utf-8') as f:
        Writer=csv.writer(File)

EDIT :

csv.writer() function takes a csvfile as argument and you are passing lists:

csv.writer(["Date","Time","Glucose"])

csv.writer(newline + main_lst)


So, the correct way is using writerow() method:

def store():
    with open('./glucose_values.csv', "a+", newline='', encoding='utf-8') as f:
        Writer=csv.writer(f)
        Writer.writerow(["Date","Time","Glucose"])
        Writer.writerow(main_lst)

Also, for appending in the CSV file, it is suggested to use DictWriter() method. Refer here.

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