在 Python 中附加数据后重新打开 CSV 的问题
我有这个工作按钮并且它确实保存了,但我的问题是我可以从同一个文件再次启动我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
for循环比您需要的更复杂,请尝试以下操作:
无需
list(range(0,len(data)))
也,请勿使用Global
文件
>。创建一种读取数据的方法,而另一种保存数据的方法,并根据需要打开文件,例如:
使用使用
您确定不使用时资源(文件)是关闭的最后
,尝试不将大写速度用于变量实例 htttps> htttps> htttps:// peps。 python.org/pep-0008/#naming-conventions
The for loop is more complex that you need, try something like:
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:
using
with
you are sure that the resource (the file) is closed when you do not use it anymoreFinally, try to not use uppercase for instances of variables https://peps.python.org/pep-0008/#naming-conventions