Python将字典数据写⼊CSV⽂件参考⼩例(待补充)
# -*- coding: utf-8 -*-
import os
import time
import csv
class WriteCSV():
"""定义成员变量"""
def__init__(self):
python怎么读csv数据pass
def init(self, info_list, info_dict_list):
self.info_list = info_list
self.info_dict_list = info_dict_list
def getBaseDir(self):
baseDir = r'D:\tempcmd\migrate'
return baseDir
def getTimestamp(self):
timestamp = time.strftime('%Y%m%d%H%M%S', time.localtime())
return timestamp
def writeFile(self):
pass
def writeFileDict(self):
filename = BaseDir()+ os.sep +'tmp'+ os.sep + Timestamp()+'.csv'
header =['姓名','年龄','性别','籍贯','电话','备注']
info_list =[{'姓名':'赵四','年龄':36,'性别':'男','籍贯':'铁岭'},
{'姓名':'刘能','年龄':38,'性别':'男','籍贯':'铁岭'}]
with open(filename,'w', newline='', encoding='utf-8')as f:
dictWriter = csv.DictWriter(f, fieldnames=header)
dictWriter.writeheader()
for item in info_list:
dictWriter.writerow(item)
# 引⽤导⼊
# writer = WriteCSV()
if __name__ =='__main__':
writer = WriteCSV()
writer.writeFileDict()
输出效果
姓名,年龄,性别,籍贯,电话,备注
赵四,36,男,铁岭,158********,⽆
刘能,38,男,铁岭,158********,⽆
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论