python操作CSV
导入依赖库import csv
写入
headers = ['用户名', '电话', '添加时间']
data = [{'用户名': '张三', '电话': '188xxx', '添加时间': '2019-1-1'}]
# data中的元素可以是字典,也可以是列表
filepath = 'xxx.csv'
with open(filepath, 'w', newline='') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()
f_csv.writerows(data)
读取
with open('xxx.csv', 'r') as f:
f_csv = csv.reader(f)
for row in f_csv:
print(row)
打印数据:
['卡密', '类型']
['6DUyZjsaQ5', '月卡']
['J1MjS7fbZX', '月卡']
['swEZR2a4c7', '月卡']
row是列表,如果想看单独的列需要加上下标