写入CSV会生成4个副本,并删除以前的现有文件

时间:2015-11-11 09:47:54

标签: csv python-3.x file-writing

所以,这段代码是一个更大的代码片段。我想要做的是在CSV文件的新行,addEv列表中的信息,新单元格中的每种类型的信息(eventDate,eventTitle,eventType,eventDescription)中编写。到目前为止,它似乎确实有效,但它创建了4行具有相同的值,如果CSV文件中已存储任何现有事件,则会删除它们。

#PLANNER
from tkinter import *
import tkinter
from csv import*
import csv

colW=['#000000','#f9f4bd','#fff200','black','#2378c8','#ed1c24']
colSch=colW

addEv=['date','title','eventtype','description']

#Page Commands
settings=False
addEvent=False
accessDayPage=False
ttt=False
bbb=False



sdDay=False
smMonth=False
syYear=False
icExistance=False
eventsCreated=False
go=False


            #addEvent

def destroyaddEvent():
    global addEvent,icExistance
    if addEvent==True:
        enterDate.destroy()
        days.destroy()
        months.destroy()
        years.destroy()
        searchDate.destroy()
        yYear.destroy()
        mMonth.destroy()
        dDay.destroy()
        eEvent.destroy()
        evTitleField.destroy()
        evType.destroy()
        evDescriptionField.destroy()
        if icExistance==True:
            evIc.destroy()
            icExistance=False
        addEvent=False

def daySet():
    global dDay,sdDay
    if sdDay==True:
        dDay.destroy()
        sdDay=False
    dDay=tkinter.Label(main,text=str(dayVar.get()),fg=colSch[3],bg=colSch[1],font=("MingLiU","14"))
    dDay.place(x=480,y=60)
    sdDay=True

def monthSet():
    global mMonth,smMonth
    if smMonth==True:
        mMonth.destroy()
        smMonth=False
    mMonth=tkinter.Label(main,text=str(monthVar.get()),fg=colSch[3],bg=colSch[1],font=("MingLiU","14"))
    mMonth.place(x=510,y=60)
    smMonth=True

def yearSet():
    global yYear,syYear
    if syYear==True:
        yYear.destroy()
        syYear=False
    yYear=tkinter.Label(main,text=str(yearVar.get()),fg=colSch[3],bg=colSch[1],font=("MingLiU","14"))
    yYear.place(x=610,y=60)
    syYear=True

def eventSet():

    global evIc,icExistance,evLogo

    if eEevType.get()==("(exam)"):
        if icExistance==True:
            evIc.destroy()
            icExistance=False
        evLogo=tkinter.PhotoImage(file="examIc.gif")
        evIc=tkinter.Label(main,image=evLogo)
        evIc.place(x=670,y=145)
        icExistance=True

    elif eEevType.get()==("(birthday)"):
        if icExistance==True:
            evIc.destroy()
            icExistance=False
        evLogo=tkinter.PhotoImage(file="birthdayIc.gif")
        evIc=tkinter.Label(main,image=evLogo)
        evIc.place(x=670,y=145)
        icExistance=True

    elif eEevType.get()==("(meeting)"):
        if icExistance==True:
            evIc.destroy()
            icExistance=False
        evLogo=tkinter.PhotoImage(file="meetingIc.gif")
        evIc=tkinter.Label(main,image=evLogo)
        evIc.place(x=670,y=145)
        icExistance=True

    elif eEevType.get()==("(school trip)"):
        if icExistance==True:
            evIc.destroy()
            icExistance=False
        evLogo=tkinter.PhotoImage(file="tripIc.gif")
        evIc=tkinter.Label(main,image=evLogo)
        evIc.place(x=670,y=145)
        icExistance=True

    elif eEevType.get()==("(coursework)"):
        if icExistance==True:
            evIc.destroy()
            icExistance=False
        evLogo=tkinter.PhotoImage(file="courseworkIc.gif")
        evIc=tkinter.Label(main,image=evLogo)
        evIc.place(x=670,y=145)
        icExistance=True




def goToDay():
    evDateSelected=str(dayVar.get()+monthVar.get()+yearVar.get()+"'")
    del addEv[0]
    addEv.insert(0,evDateSelected)

    del addEv[1]
    addEv.insert(1,evTitleField.get())

    del addEv[2]
    addEv.insert(2,eEevType.get())

    del addEv[3]
    addEv.insert(3,evDescriptionField.get("1.0",'end-1c'))

    print (addEv)
    eventsFile=open("dayFile.csv","w",newline='')
    evWriter=csv.writer(eventsFile)
    for i in addEv:
        evWriter.writerows([addEv])



def addEvent():
    destroyaddEvent()

    global enterDate,addEvent,evDescriptionField,days,months,years,searchDate,dayVar,monthVar,yearVar,dDay,mMonth,yYear,evTitleField,eEevType,eEvent,evType

    enterDate=tkinter.Label(main,text="Please enter date (dd/mm/yyyy):",fg=colSch[5],bg=colSch[0],font=("MingLiU","14"))
    enterDate.place(x=140,y=20)


    days=tkinter.Menubutton(main,text="DD",relief=RAISED,width=5,height=1, font=("Dotum","11"))
    days.place(x=480,y=20)
    days.menu=tkinter.Menu(days,tearoff=0)
    days['menu']=days.menu

    dayVar=StringVar()
    days.menu.add_radiobutton(label=str(1),variable=dayVar,value=str(1),command=daySet)
    days.menu.add_radiobutton(label=str(2),variable=dayVar,value=str(2),command=daySet)
    days.menu.add_radiobutton(label=str(3),variable=dayVar,value=str(3),command=daySet)
    days.menu.add_radiobutton(label=str(4),variable=dayVar,value=str(4),command=daySet)
    days.menu.add_radiobutton(label=str(5),variable=dayVar,value=str(5),command=daySet)
    days.menu.add_radiobutton(label=str(6),variable=dayVar,value=str(6),command=daySet)
    days.menu.add_radiobutton(label=str(7),variable=dayVar,value=str(7),command=daySet)
    days.menu.add_radiobutton(label=str(8),variable=dayVar,value=str(8),command=daySet)
    days.menu.add_radiobutton(label=str(9),variable=dayVar,value=str(9),command=daySet)
    days.menu.add_radiobutton(label=str(10),variable=dayVar,value=str(10),command=daySet)
    days.menu.add_radiobutton(label=str(11),variable=dayVar,value=str(11),command=daySet)
    days.menu.add_radiobutton(label=str(12),variable=dayVar,value=str(12),command=daySet)
    days.menu.add_radiobutton(label=str(13),variable=dayVar,value=str(13),command=daySet)
    days.menu.add_radiobutton(label=str(14),variable=dayVar,value=str(14),command=daySet)
    days.menu.add_radiobutton(label=str(15),variable=dayVar,value=str(15),command=daySet)
    days.menu.add_radiobutton(label=str(16),variable=dayVar,value=str(16),command=daySet)
    days.menu.add_radiobutton(label=str(17),variable=dayVar,value=str(17),command=daySet)
    days.menu.add_radiobutton(label=str(18),variable=dayVar,value=str(18),command=daySet)
    days.menu.add_radiobutton(label=str(19),variable=dayVar,value=str(19),command=daySet)
    days.menu.add_radiobutton(label=str(20),variable=dayVar,value=str(20),command=daySet)
    days.menu.add_radiobutton(label=str(21),variable=dayVar,value=str(21),command=daySet)
    days.menu.add_radiobutton(label=str(22),variable=dayVar,value=str(22),command=daySet)
    days.menu.add_radiobutton(label=str(23),variable=dayVar,value=str(23),command=daySet)
    days.menu.add_radiobutton(label=str(24),variable=dayVar,value=str(24),command=daySet)
    days.menu.add_radiobutton(label=str(25),variable=dayVar,value=str(25),command=daySet)
    days.menu.add_radiobutton(label=str(26),variable=dayVar,value=str(26),command=daySet)
    days.menu.add_radiobutton(label=str(27),variable=dayVar,value=str(27),command=daySet)
    days.menu.add_radiobutton(label=str(28),variable=dayVar,value=str(28),command=daySet)
    days.menu.add_radiobutton(label=str(29),variable=dayVar,value=str(29),command=daySet)
    days.menu.add_radiobutton(label=str(30),variable=dayVar,value=str(30),command=daySet)
    days.menu.add_radiobutton(label=str(31),variable=dayVar,value=str(31),command=daySet)


    months=Menubutton(main,text="MM",relief=RAISED,width=5,height=1, font=("Dotum","11"))
    months.place(x=540,y=20)
    months.menu=tkinter.Menu(months,tearoff=0)
    months['menu']=months.menu

    monthVar=StringVar()
    months.menu.add_radiobutton(label=str("January"),variable=monthVar,value=("January"),command=monthSet)
    months.menu.add_radiobutton(label=str("February"),variable=monthVar,value=("February"),command=monthSet)
    months.menu.add_radiobutton(label=str("March"),variable=monthVar,value=("March"),command=monthSet)
    months.menu.add_radiobutton(label=str("April"),variable=monthVar,value=("April"),command=monthSet)
    months.menu.add_radiobutton(label=str("May"),variable=monthVar,value=("May"),command=monthSet)
    months.menu.add_radiobutton(label=str("June"),variable=monthVar,value=("June"),command=monthSet)
    months.menu.add_radiobutton(label=str("July"),variable=monthVar,value=("July"),command=monthSet)
    months.menu.add_radiobutton(label=str("August"),variable=monthVar,value=("August"),command=monthSet)
    months.menu.add_radiobutton(label=str("September"),variable=monthVar,value=("September"),command=monthSet)
    months.menu.add_radiobutton(label=str("October"),variable=monthVar,value=("October"),command=monthSet)
    months.menu.add_radiobutton(label=str("November"),variable=monthVar,value=("November"),command=monthSet)
    months.menu.add_radiobutton(label=str("December"),variable=monthVar,value=("December"),command=monthSet)


    years=Menubutton(main,text="YYYY",relief=RAISED,width=5,height=1, font=("Dotum","11"))
    years.place(x=600,y=20)
    years.menu=tkinter.Menu(years,tearoff=0)
    years['menu']=years.menu

    yearVar=StringVar()
    years.menu.add_radiobutton(label=str("2015"),variable=yearVar,value=("2015"),command=yearSet)
    years.menu.add_radiobutton(label=str("2016"),variable=yearVar,value=("2016"),command=yearSet)
    years.menu.add_radiobutton(label=str("2017"),variable=yearVar,value=("2017"),command=yearSet)
    years.menu.add_radiobutton(label=str("2018"),variable=yearVar,value=("2018"),command=yearSet)
    years.menu.add_radiobutton(label=str("2019"),variable=yearVar,value=("2019"),command=yearSet)
    years.menu.add_radiobutton(label=str("2020"),variable=yearVar,value=("2020"),command=yearSet)
    years.menu.add_radiobutton(label=str("2021"),variable=yearVar,value=("2021"),command=yearSet)
    years.menu.add_radiobutton(label=str("2022"),variable=yearVar,value=("2022"),command=yearSet)


    searchDate=tkinter.Button(main,text="CREATE",bg=colSch[2],fg=colSch[5],font=("Dotum","14"),command=goToDay)
    searchDate.place(x=680,y=38)

    dDay=tkinter.Label(main,text='.',fg=colSch[0],bg=colSch[0],font=("MingLiU","1"))
    dDay.place(x=600,y=400)
    mMonth=tkinter.Label(main,text='.',fg=colSch[0],bg=colSch[0],font=("MingLiU","1"))
    mMonth.place(x=600,y=400)
    yYear=tkinter.Label(main,text='.',fg=colSch[0],bg=colSch[0],font=("MingLiU","1"))
    yYear.place(x=600,y=400)
    eEvent=tkinter.Label(main,text='.',fg=colSch[0],bg=colSch[0],font=("MingLiU","1"))
    eEvent.place(x=600,y=400)



    evTitleField=tkinter.Entry(main,width=85)
    evTitleField.place(x=140,y=115)

    evType=Menubutton(main,text="Event Type",relief=RAISED, font=("Dotum","11"))
    evType.place(x=680,y=115)
    evType.menu=tkinter.Menu(evType,tearoff=0)
    evType['menu']=evType.menu


    eEevType=StringVar()
    evType.menu.add_radiobutton(label=str("Birthday"),variable=eEevType,value=("(birthday)"),command=eventSet)
    evType.menu.add_radiobutton(label=str("Exam"),variable=eEevType,value=("(exam)"),command=eventSet)
    evType.menu.add_radiobutton(label=str("School trip"),variable=eEevType,value=("(school trip)"),command=eventSet)
    evType.menu.add_radiobutton(label=str("Coursework"),variable=eEevType,value=("(coursework)"),command=eventSet)
    evType.menu.add_radiobutton(label=str("Meeting"),variable=eEevType,value=("(meeting)"),command=eventSet)

    evDescriptionField= Text(main,width=64,height=12)
    evDescriptionField.place(x=140,y=140)






    addEvent=True

main=tkinter.Tk()
main.configure(bg=colSch[0])
main.geometry('780x375')
w=Canvas(main,width=780,height=375)
w.pack()
rect1=w.create_rectangle(0,0,775,375,fill=colSch[0])
rect2=w.create_rectangle(0,0,120,375,fill=colSch[1])


cal=tkinter.Button(main,text="Add Event",bg=colSch[2],fg=colSch[4],width=15,height=3,relief=FLAT, font=("Bernard MT Condensed","11"),command=addEvent)
cal.place(x=5,y=80)





main.mainloop()

目的是创建一个包含不同事件的CSV文件,例如:enter image description here

PS:我知道格式化可以做很多改进,但这是我编码的第一年。

由Francisco Javier Lopez-Hernandez编码。

1 个答案:

答案 0 :(得分:0)

问题是由goToDay()函数中的此代码引起的:

eventsFile=open("dayFile.csv","w",newline='')
evWriter=csv.writer(eventsFile)
for i in addEv:
    evWriter.writerows([addEv])

...因为addEv中有4个项目,每次调用该函数时,它会从头开始重写文件并输出相同的数据行(列表中当前的4个项目){{1} (4)次。

我不确定如何修复它,因为我不清楚你想要发生什么。同时,我建议您暂停编码并花更多时间学习Python for i in addEv:循环。您的代码中有许多地方使用它们会大大简化和缩短您的代码(尽管这与您的问题中的问题无关)。