本文实例为大家分享了三种方式使用python写数据到csv或xlsx文件,供大家参考,具体内容如下
第一种:使用csv模块,写入到csv格式文件
# -*- coding: utf-8 -*-
import 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)
第二种:使用openpyxl模块,写入到xlsx格式文件
# -*- coding: utf-8 -*-
import openpyxl as xl
import os
def write_excel_file(folder_path):
result_path = os.path.join(folder_path, "my.xlsx")
print(result_path)
print('***** 开始写入excel文件 ' + result_path + ' ***** \n')
if os.path.exists(result_path):
print('***** excel已存在,在表后添加数据 ' + res