修复 llm_result.json 文件以供 json.load() 解析
修复 llm_result.json 文件以供 json.load() 解析\n\n这段代码会打开llm_result.json文件,并读取其中的内容。然后,它会找到最后一个"}",并删除该行及其之后的所有行。接着,它会将最后一行的最后两个字符删除,并添加一个新的"}"行。最后,它会将修改后的内容写回到llm_result.json文件中。\n\n这样修改后的llm_result.json文件就可以被json.load()方法解析了。\n\npython\nwith open('llm_result.json') as f:\n lines = f.readlines()\n index = len(lines) - 1\n while index >= 0:\n if lines[index].strip() == "},":\n break\n index -= 1\n del lines[:index]\n lines[-1] = lines[-1][:-2]\n lines.append(' }')\nwith open('llm_result.json', 'w') as file:\n file.writelines(lines)\n
原文地址: https://www.cveoy.top/t/topic/qrRO 著作权归作者所有。请勿转载和采集!