python - 试图让 Tkinter 中的按钮重写 csv 文件
问题描述
想要使我放置的按钮在该人旁边写入/添加一个新列(我已经制作了识别该人的代码)但我试图允许用所有旧信息重写 csv 文件,用户将从 3 个按钮中选择一个附加列来选择填充,这是我迄今为止尝试过的。
#opening the csv file
filepath = 'file name'
csvFile = open(filepath)
#reader through the csv file
reader = csv.reader(csvFile)
# turning the csv file into a list.
Data = list (reader)
list_of_entries = []
list_of_entries.append(Data["Time"])
def TimeBTN15 ():
Data["Time"] = '15 mins'
Data["Time"] = newtime
def TimeBTN30 ():
Data["Time"] = '30 mins'
Data["Time"] = newtime
def TimeBTN1 ():
Data["Time"] = '1 Hour'
Data["Time"] = newtime
TimeLBL = Label(frame3,text="What time would you like to pick up meal:",font=('Arial',18, "bold"),bg = '#F0EAD6')
TimeLBL.place(x= 80, y= 575)
TimenBTN15 = Button(frame, text = '15 mins', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN15 )
TimenBTN15.place(x= 450, y= 575)
TimenBTN30 = Button(frame, text = '30 min ', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN30 )
TimenBTN30.place(x= 525, y= 575)
TimenBTN1 = Button(frame, text = ' 1 hour', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN1 )
TimenBTN1.place(x= 600, y= 575)
csvFile = open('shopoutput.csv', 'w+')
# writting in the headers
csvFile.write("Name,Food,Drinks,Price,NewTime\n")
#adding all the new data into the orginal and new data into the shopoutput csv
for data in list_of_entries:
csvFile.write(data['Name'] + ',')
csvFile.write(data['Food'] + ',')
csvFile.write(data['Drinks'] + ',')
csvFile.write(data['Price'] + ',')
csvFile.write(data['Time'] + '\n')
csvFile.close()
解决方案
很难提供帮助,因为您没有提供完整的程序。你在这里有很多问题。 Data
是字典列表,而不是字典。您永远不会将任何内容复制到list_of_entries
中,因此永远不会有您的旧数据。在用户做出一些选择之前,您不能向 csv 文件写入任何内容——它必须在按钮响应中。这是一个似乎更接近您想要的示例。
#opening the csv file
filepath = 'file name'
with open(filepath) as csvfile:
csvFile = open(filepath)
# Read the csv file
Data = csv.reader(csvFile)
# Add Time to each entry
for row in Data:
row['Time'] = ''
def writecsv():
with open('shopoutput.csv', 'w+') as xfile:
csvFile = csv.DictWriter(xfile, Data[0].keys())
csvFile.writerows( Data )
def findPerson():
for row in Data:
if row['Name'] == targetname:
return row
return None
def TimeBTN15 ():
row = findPerson()
row["Time"] = '15 mins'
writecsv()
sys.exit()
def TimeBTN30 ():
row = findPerson()
row["Time"] = '30 mins'
writecsv()
sys.exit()
def TimeBTN1 ():
row = findPerson()
row["Time"] = '1 Hour'
writecsv()
sys.exit()
TimeLBL = Label(frame3,text="What time would you like to pick up meal:",font=('Arial',18, "bold"),bg = '#F0EAD6')
TimeLBL.place(x= 80, y= 575)
TimenBTN15 = Button(frame, text = '15 mins', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN15 )
TimenBTN15.place(x= 450, y= 575)
TimenBTN30 = Button(frame, text = '30 min ', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN30 )
TimenBTN30.place(x= 525, y= 575)
TimenBTN1 = Button(frame, text = ' 1 hour', font=('Arial',16,),bg = '#F0EAD6',command = TimeBTN1 )
TimenBTN1.place(x= 600, y= 575)
推荐阅读
- firebase - 只要应用程序打开并连接,Firestore 侦听器是否会无限期运行?
- arrays - 将数据从哈希插入到 Ruby 中设置索引处的数组中
- javascript - if函数中的JS简单布尔语句-总是得到TRUE值
- java - 如何从“ACTION_IMAGE_CAPTURE”的活动结果中获取路径?
- javascript - Javascript 根据数组中的字符串列表过滤对象的最佳方法是什么?
- python - 格式化字符串到 dict
- ruby-on-rails - 将 Rails 应用程序部署到 Heroku 时出现 NoMethodError(在模型上调用 .create 时)
- flutter - 如何为 FCM 推送通知设置字幕/字幕?
- php - Gettig 以分层形式包含子类别的所有类别帖子
- python - 在 pandas 中使用 read_csv 和 to_csv 时如何保留 csv 数据