person_info = [{"name": "可优",
"age": 17,
"gender": "男",
"hobby": "臭美",
"motto": "Always Be Coding!"},
{"name": "柠檬小姐姐",
"age": 16,
"gender": "女",
"hobby": "可优",
"motto": "Lemon is best!"},
]
file_path = "test_data.txt"
one_dict =person_info[0]
keys_list = list(one_dict)
first_line = ",".join(keys_list)+'\n'
def write_file(file_path,data,mode="a"):
one_file = open(file_path,encoding="utf8",mode=mode)
one_file.write(data)
one_file.close()
write_file(file_path,first_line)
def handle_data(one_list):
datas_str =""
for item in one_list:
tmp_list =[]
for i in item.values():
tmp_list.append(str(i))
datas_str = datas_str +','.join(tmp_list)+"\n"
return datas_str
write_datas = handle_data(person_info)
write_file(file_path,write_datas)
with open(file_path,encoding="utf8") as one_file:
contents = one_file.read()
print(contents)
版权声明:本文为weixin_42331253原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。