python 将字典dict保存到csv文件中
方法一
saveDict={"用户1":"密码1","用户2":"密码2"}
fileName="filename.csv"
##保存文件
with open(fileName,"wb") as csv_file:
writer=csv.writer(csv_file)
for key,value in saveDict.items:
writer.writerow([key,value])
##读取文件
readDict={}
with open(fileName,"rb") as csv_file:
reader=csv.reader(csv_file):
readDict=dict(reader)
方法二
with open('my_file.csv', 'w') as f:
[f.write('{0},{1}\n'.format(key, value)) for key, value in my_dict.items()]