Python习题集(一)

每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我!

https://www.cnblogs.com/poloyy/category/1676599.html

题目

有一个数据list of dict如下

a = [

    {"test1": "123456"},

    {"test2": "123456"},

    {"test3": "123456"},

]

写入到本地一个txt文件,内容格式如下:

test1,123456

test2,123456

test3,123456

解题思路

  1. 打开文件
  2. 循环列表,提取字典
  3. 提取key,value
  4. 写入文件

答案

lists = [
    {"yoyo1": "111111"},
    {"yoyo2": "222222"},
    {"yoyo3": "333333"},
]

with open("test.txt", "w+", encoding="utf-8") as f:
    for data in lists:
        for key, value in data.items():
            f.write(f"{key},{value}
")
 
原文地址:https://www.cnblogs.com/poloyy/p/12539215.html