# 是否保存 save = input("共" + str(len(comment_list)) + "条记录,保存到commen.txt(y/n):") if (save == "y"or save == "Y"): print("正在写入中...") withopen("comment.txt", "w", encoding="utf-8") as f: for comment in tqdm(comment_list): f.writelines(comment + "\n")
对于字典
1 2 3 4 5 6 7
# 是否保存? save = input("共" + str(len(list_dic_blog)) + "条记录,保存到blog.txt(y/n):") if (save == "y"or save == "Y"): withopen("blog.txt", "w", encoding='Utf-8') as f: for dic_blog in tqdm(list_dic_blog): f.writelines(str(dic_blog) + "\n")
二. JSON
1 2 3 4
json_str = json.dumps(the_dict,indent=4,ensure_ascii=False) withopen(file_name, 'w') as json_file: json_file.write(json_str)