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)
os.模块得写入与读笔记
最新推荐文章于 2023-08-03 11:09:48 发布