1. 程式人生 > >python csv實時一條一條插入且表頭不重複方法

python csv實時一條一條插入且表頭不重複方法

1,主要就是在最開始插入的時候判斷下表頭或者表格是否有資料,
2,用csv reader(然後主要寫是a追加模式,讀是r模式)

import csv

with open("111.csv","a",encoding='utf-8',newline="") as f:


     k = csv.writer(f, dialect="excel")
      with open("111.csv", "r", encoding='utf-8', newline="") as f:
          reader = csv.reader(f)
          if not [row for row in reader]:
              k.writerow(["標題", "連結", "喜歡"])
              k.writerow(list)
          else:
              k.writerow(list)