python 读写csv文件(创建,追加,覆盖)

1.创建:
创建的形式打开其实就是覆盖了

import csv
def create_csv():
    path = "ecg.csv"
    with open(path,'w') as f:
        csv_write = csv.writer(f)
        csv_head = ["id","label"]
        csv_write.writerow(csv_head)

2.追加:

def write_csv():
    path  = "ecg.csv"
    with open(path,'a+') as f:
        csv_write = csv.writer(f)
        data_row = ["1dfg5llw","0,1,0,1,0,0"]
        csv_write.writerow(data_row)

3.读:

def read_csv():
    path = "ecg.csv"
    with open(path,"r") as f:
        csv_read = csv.reader(f)
        for line in csv_read:
            print(line)
posted @ 2022-01-21 18:53  小Aer  阅读(28)  评论(0编辑  收藏  举报  来源