python3中列表数据保存到csv文件

python中保存列表数据到csv文件和xlsx文件

python -V 3.7

分三种:

第一种保存好几个列表数据到csv的好几列

import csv

li = [[1, 'cheng', 'A','True'], [2, 'wei', 'B','False'], [3, 'wang', 'C','False']]
with open("save_usename_team.csv", "w", newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["index", "usename", "team",'admin'])   # 先写入columns_name
    writer.writerows(li)   #写入多行用writerows

第二种,就保存一个列表数据到csv文件

i = ['a','b','c']
with open("save_team_admin.csv", "w", newline='') as csvfile:
    for i in li:
        csvfile.write(i)
        csvfile.write('\n')

第三种

import pandas as pd

li = [[1, 'cheng', 'A','True'], [2, 'wei', 'B','False'], [3, 'wang', 'C','False']]
columns = ["index", "usename", "team",'admin']
dt = pd.DataFrame(li, columns=columns)
dt.to_excel("save_usename_team.xlsx", index=0)
# dt.to_csv("result_csv.csv", index=0)

你可能感兴趣的:(数据处理)