# read from file
code = [line.strip() for line in open(file_path)] # file content: "var x = '\n'"
# print(code) now looks like: ["var x = '\\n'"]
# write json to disk
with open('json.cfg', 'w') as file:
file.write(json.dumps(code))
>> cat json.cfg
>> "var x = '\\n'"
我希望json.cfg
内容为:"var x = '\n'"