import json def txt_to_json(file_path): with open(file_path, "r") as file: lines = file.readlines() data = {} for line in lines: key, value = line.strip().split(":") data[key] = value json_data = json.dumps(data, indent=4) with open(file_path.replace(".txt", ".json"), "w") as file: file.write(json_data) file_path = "example.txt" txt_to_json(file_path)
标签:open,headers,json,file,path,txt,data From: https://www.cnblogs.com/dashenblog/p/17084595.html