1. 程式人生 > >【Python】向json檔案中追加新的物件

【Python】向json檔案中追加新的物件

def get_json(path, write_path): file = open(path, encoding=‘utf8’, errors=‘ignore’) file_lines = file.read() file.close() file_json = json.loads(file_lines) json_txt = [] for json_data in file_json: if isinstance(json_data, dict): print(json_data) nums = random.randint(1, 100) json_data[“number”] = str(nums) json_txt.append(json_data)