创建/将新字典附加到json的最佳方法,以便稍后可以在python中将其轻松转换为df

时间:2018-11-01 14:39:51

标签: python json python-3.x

我正在尝试将数据从简单的字典保存到json文件,并尝试稍后读取以进行下游处理。问题是我可以保存数据,但是以后无法将其转换为python数据框。

示例数据和代码:

def new_data(a, b, c, d):
    info = dict()
    info['A'] = a
    info['B'] = b
    info['C'] = c
    info['D'] = d
    return info

cat1 = new_data("Python", "2", "old", "not compatible")
cat2 = new_data("Python", "3", "new", "compatible")

file_path = "Programs.json"
with open(file_path, 'a') as f:
    f.write(json.dumps(cat1))
with open(file_path, 'a') as f:
    f.write(json.dumps(cat2))

with open(file_path, "r") as file:
    data = json.load(file)

我收到以下错误:

json.decoder.JSONDecodeError: Extra data: line 1 column 61 (char 60)

2 个答案:

答案 0 :(得分:2)

如果要附加到JSON文件,我可能建议使用逐行JSON:

with open(file_path, "a") as f:
    f.write(json.dumps(cat1) + "\n")  # note \n = line break

读取此文件时,需要逐行读取和解码它:

docs = []
with open(file_path, "r") as f:
    for line in f:
        doc = json.loads(line)
        print(doc)
        docs.append(doc)

答案 1 :(得分:1)

如果您仅向现有json文件追加更多json数据,则将创建无效的json-您需要加载现有文件,将对象与新对象一起放入列表中,然后将对象列表写入文件中:

import json

def new_data(a, b, c, d):
    info = dict()
    info['A'] = a
    info['B'] = b
    info['C'] = c
    info['D'] = d
    return info

cat1 = new_data("Python", "2", "old", "not compatible")
cat2 = new_data("Python", "3", "new", "compatible")

file_path = "another_Programs.json"

with open(file_path,"w") as f:
    f.write(json.dumps([cat1,cat2])) # store them as list of json dicts

with open(file_path,"r") as f: 
    print(json.load(f))

输出:

[{'A': 'Python', 'B': '2', 'C': 'old', 'D': 'not compatible'}, 
 {'A': 'Python', 'B': '3', 'C': 'new', 'D': 'compatible'}]

该错误告诉您更多信息:读取其中一些数据后还有其他数据:

json.decoder.JSONDecodeError: **Extra data: line 1 column 61** (char 60)

{'A': 'Python', 'B': '2', 'C': 'old', 'D': 'not compatible'}{'A': 'Python', 'B': '3', 'C': 'new', 'D': 'compatible'}的文件内容不是有效的json。

相关问题