csv.writer(fp)以元组方式写入
def write_csv_demo1():
header = ["username", "age", "height"]
value = [
("赵吉", 25, 180),
("程煜晶", 25, 166)
]
with open("./data/1.csv", "w", encoding="utf-8", newline="") as fp:
write = csv.writer(fp)
write.writerow(header)
write.writerows(value)
fp.close()
csv.DictWriter(fp, header)以字典方式写入
def write_csv_demo2():
header = ["username", "age", "height"]
values = [
{
"username": "赵吉", "age":25, "height": 185
},
{
"username": "程煜晶", "age": 25, "height": 166
}
]
with open("./data/2.csv", "w", encoding="utf-8", newline="") as fp:
write = csv.DictWriter(fp, header)
write.writeheader()
write.writerows(values)
fp.close()