将JSON转换为可变txt,然后返回JSON

2024-06-17 12:13:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON文件,我必须将字符串[1]的每个实例都更改为\u e。你知道吗

  #!/usr/bin/env python3
import fileinput

with fileinput.FileInput('reactions.json', inplace=True, backup='.bak') as file:
    for line in file:
    print(line.replace('[0]', '_c'), end='')
    print(line.replace('[1]', '_e'), end='')


with open('reactions.json') as data_file:    
data_reactions = json.load(data_file)

这就像一个魅力,但一旦我重命名文件扩展名为txt,该文件就不能再保存为JSON并正确读取。有没有办法改回来?我注意到将其保存为txt文件似乎会删除ENTER分隔符。。。我想。你知道吗


Tags: 文件字符串txtjsondataaswithline