#写
def write_dict():
with open('phone.csv','w')as f:
writer = csv.DictWriter(f, fieldnames=("id",'name','phone'))
writer.writeheader() #写入头
writer.writerow({'id':101,'name':'swt','phone':'123456789'})
writer.writerow({'id':102,'name':'zs','phone':'123451111'})
writer.writerow({'id':103,'name':'lt','phone':'123422229'})
writer.writerow({'id':104,'name':'bw','phone':'1234563333'})
print('写入成功')
#读
with open('phone.csv','r')as f:
reader = csv.DictReader(f,fieldnames=('id','name','phone'))
for i in reader:
print(i)
得出的结果为:
OrderedDict([('id', 'id'), ('name', 'name'), ('phone', 'phone')])
OrderedDict([('id', '101'), ('name', 'swt'), ('phone', '123456789')])
OrderedDict([('id', '102'), ('name', 'zs'), ('phone', '123451111')])
OrderedDict([('id', '103'), ('name', 'lt'), ('phone', '123422229')])
OrderedDict([('id', '104'), ('name', 'bw'), ('phone', '1234563333')])