python csv,仅一次写入标头

python csv,仅一次写入标头,第1张

python csv,仅一次写入标头

您可以检查文件是否已经存在,然后不调用

writeheader()
因为您使用附加选项打开文件。

像这样:

import os.pathfile_exists = os.path.isfile(filename)with open (filename, 'a') as csvfile:    headers = ['TimeStamp', 'light', 'Proximity']    writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='n',fieldnames=headers)    if not file_exists:        writer.writeheader()  # file doesn't exist yet, write a header    writer.writerow({'TimeStamp': dic['ts'], 'light': dic['light'], 'Proximity': dic['prox']})


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5662702.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存