列表如下:
a = [ {"name1":"123"}, {"name2":"456"}, {"name3":"789"}, ]
解题思路:
- 打开文件
- 循环列表,提取字典
- 提取key,value
- 写入文件
代码如下:
a = [ {"name1":"123"}, {"name2":"456"}, {"name3":"789"}, ] with open("test66.txt","a") as f: for data in a: for key,value in data.items(): f.write("\n"+f"{key},{value}"+"\n")
结果:
成功输出打印!
name1,123 name2,456 name3,789
标签:写入,456,列表,123,name2,name3,key,txt From: https://www.cnblogs.com/LoLong/p/16796675.html