Python 将 数据list写入本地文件

'''
    有⼀个数据list of dict如下
    a = [
                {"yoyo1": "123456"},
                {"yoyo2": "123456"},
                {"yoyo3": "123456"},
        ]
            写⼊到本地⼀个txt⽂件,内容格式如下:
                                            yoyo1,123456
                                            yoyo2,123456
                                            yoyo3,123456

'''
def test():

a = [
{"yoyo1": "123456"},
{"yoyo2": "123456"},
{"yoyo3": "123456"},
]

# 打开一个名为 test.txt的文件,如果文件不存在,则自动创建

with open('test1.txt','w') as f:

for i in a:

for key,value in i.items():

f.write("{0},{1}\n".format(key,value))
print("{0},{1}\n".format(key,value))



test()

 

 



posted @ 2022-05-06 14:36  大熊童鞋  阅读(688)  评论(0编辑  收藏  举报