CSV文件數據寫入簡單示例
import csv
# 示例,在同級目錄下 自動創建my.csv文件,寫入數據
with open('my.csv', "a", newline='') as f:
writer = csv.writer(f)
writer.writerow(["URL", "predict", "score"])
row = [['1', 1, 1], ['2', 2, 2], ['3', 3, 3]]
for r in row:
writer.writerow(r)
# 自定義路徑,寫入數據
ave_per = 32.658
delta_path = r'C:\Users\weiwei\Desktop\my.csv'
delta = format(ave_per - 31.667, '.3f')
with open(delta_path, "a", newline='') as f:
writer = csv.writer(f)
writer.writerow([delta])