CSV搜索并替换特定的行或列

时间:2018-08-16 11:43:35

标签: python-3.x csv tkinter

我搜索了一段时间,而我的问题没有任何解决方案。 我希望有人能帮助我一点。 首先,我是pyhton的初学者。(3.7)

我的程序应该执行以下操作: 一位客户来到公司并注册了姓名,邮件和ID(= 1) 如果客户已经存在,则ID应计数为10(ID + 1)

到目前为止,可以将客户添加到csv中。 当客户已经在csv中时,缺少的部分。 我知道ID总是在同一列中,所以我只想替换csv中的ID。 CSV看起来像这样: 姓,名,邮件,电话,ID Max,Mustermann,max @ test.com,000,1

    def button_action():
Vorname = eingabe_vorname.get()
Nachname = eingabe_nachname.get()
mail = eingabe_mail.get()
tel = eingabe_tel.get()
#ID = 1
if (Vorname == "" or Nachname == "" or mail == ""):
namen_label.config(text="Bitte fülle alle Felder aus.")
else:
infos=(Vorname,Nachname,mail,tel,ID)
#checks if the name is already knwown
#counts purchases until 10th, than resets the counter
with open('C:\\temp\\csv.csv') as csvfile:
list = csv.reader(csvfile,delimiter=",")
for row in list:
if(len(row) > 4):
if (Vorname ==row[0] and Nachname == row[1] and mail == row[2] and tel == row[3]):
namen_label.config(text='vorhanden')
einkauf = int(row[4])
if einkauf <9:
einkauf_neu = einkauf + 1
namen_label.config(text=('Kunde bereits vorhanden, bereits der ',einkauf_neu,' Einkauf'))
else:
namen_label.config(text='Bereits der 10 Einkauf,gratis gift')
einkauf_neu = 0
new_entry = (Vorname,Nachname,mail,tel,einkauf_neu)
#overwrite of theID 
with open ('C:\\temp\\csv1.csv','w') as output_exist:
writeexist = csv.writer(output_exist)
writeexist.writerow(new_entry)#where? I want to write in the same column where the name isI tried to finish it on my own but i struggle with the replace of the ID

我想在没有像熊猫这样的额外模块的情况下完成它。

如果有人给我小费,那就太好了。

0 个答案:

没有答案