在 Python 中使用 TKinter 保存并向 csv 文件添加新行
因此,我包含了一些无法从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
w+
打开文件以进行写入和读取。 如果文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件以供读写。因此,请改用
a
,因为使用a
参数打开文件可让您追加到文件末尾,而不是简单地覆盖现有内容。编辑:
csv.writer()
函数接受 csvfile 作为参数,并且您正在传递列表:因此,正确的方法是使用
writerow()
方法:另外,为了追加到 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 thea
parameter allows you to append to the end of the file instead of simply overwriting the existing content.EDIT :
csv.writer()
function takes a csvfile as argument and you are passing lists:So, the correct way is using
writerow()
method:Also, for appending in the CSV file, it is suggested to use
DictWriter()
method. Refer here.