Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CSV不断覆盖_Python_Excel_Csv_Tkinter_Overwrite - Fatal编程技术网

Python CSV不断覆盖

Python CSV不断覆盖,python,excel,csv,tkinter,overwrite,Python,Excel,Csv,Tkinter,Overwrite,我正在尝试用Python为我的父母制作一个快速记账程序。我希望它如何工作如下。每次我父母打开这个程序,我都希望它在excel文件中产生一个新行。问题是程序每次运行时都会覆盖自身。下面是我的代码 from tkinter import * import csv import pandas as pd root = Tk() root.title('Boekhouding 2020') root.minsize(250, 100) #Weeknummer Label1 = Label(root,

我正在尝试用Python为我的父母制作一个快速记账程序。我希望它如何工作如下。每次我父母打开这个程序,我都希望它在excel文件中产生一个新行。问题是程序每次运行时都会覆盖自身。下面是我的代码

from tkinter import *
import csv
import pandas as pd

root = Tk()
root.title('Boekhouding 2020')
root.minsize(250, 100)
#Weeknummer
Label1 = Label(root, text= "Weeknummer").grid(row=1, column=1)
e1 = Entry(root)
e1.grid(row=1, column=2)
#Omschrijving
Label2 = Label(root, text= "Omschrijving").grid(row=2, column=1)
e2 = Entry(root)
e2.grid(row=2, column=2)
#Bedrag
Label3 = Label(root, text= "Bedrag").grid(row=3, column=1)
e3 = Entry(root)
e3.grid(row=3, column=2)



##  Exporteren naar Excel
def export(oms,bedrag,weeknummer):
    with open(r"C:\Users\frank\Desktop\Boekhouding.csv", 'w', newline='') as f:
        thewriter = csv.writer(f)
        row_export()
        thewriter.writerow([oms,bedrag,weeknummer])

## Klik definition
def myclick():
    mylabel = Label(root, text='Hello ' + e1.get())
    mylabel.grid(row=5, column=1)
    export(e2.get(),e3.get(),e1.get())

## Rowcounter
def row_export():
    with open(r"C:\Users\frank\Desktop\Boekhouding.csv",'r')as csv_file:
        fileObject = csv.reader(csv_file)
        for row in fileObject:
            print(row)
            export(row[0],row[1],row[2])



## All buttons
mybutton = Button(root, text="exporteren naar excel", command=myclick)
mybutton.grid(row=4, column=1)

root.mainloop()

尝试使用“a”而不是“w”,并告知发生的情况。 “w”仅用于写入,光标不会移动到文件的末尾,因此它会覆盖


根据Python的定义:

字符含义
“r”打开进行读取(默认)
“w”打开以进行写入,首先截断文件
“x”打开以独占方式创建,如果文件已存在,则失败
“a”打开进行写入,如果文件存在,则追加到文件末尾
“b”二进制模式
“t”文本模式(默认)
“+”打开以进行更新(读写)
使用正确的模式<代码>'w'总是以空文件开头
'a'
是始终写入文件末尾的正确模式。

请阅读
Character Meaning
   'r'    open for reading (default)
   'w'    open for writing, truncating the file first
   'x'    open for exclusive creation, failing if the file already exists
   'a'    open for writing, appending to the end of the file if it exists
   'b'    binary mode
   't'    text mode (default)
   '+'    open for updating (reading and writing)