我正在尝试编写一个Python脚本,合并两个JSON文件,例如:
第一个文件:students.json
第二个文件:teacher.json
所以首先,为了不丢失任何信息,我修改文件并添加每个人的状态,就像这样:
我做了以下代码来实现这个:
我想做的是将data_student和data_teacher合并,并将结果JSON打印到一个JSON文件中,但我只能使用标准库、pandas、numpy和scipy。
经过一些测试,我意识到有些老师也是学生,这可能会对合并造成问题。
第一个文件:students.json
{"John Smith":{"age":16, "id": 1}, ...., "Paul abercom":{"age":18, "id": 764}}
第二个文件:teacher.json
{"Agathe Magesti":{"age":36, "id": 765}, ...., "Tom Ranliver":{"age":54, "id": 801}}
所以首先,为了不丢失任何信息,我修改文件并添加每个人的状态,就像这样:
{"John Smith":{"age":16, "id": 1, "status":"student"}, ...., "Paul abercom":{"age":18, "id": 764, "status":"student"}}
{"Agathe Magesti":{"age":36, "id": 765, "status":"teacher"}, ...., "Tom Ranliver":{"age":54, "id": 801, "status":"teacher"}}
我做了以下代码来实现这个:
import pandas as pd
type_student = pd.read_json('student.json')
type_student.loc["status"] = "student"
type_student.to_json("testStudent.json")
type_teacher = pd.read_json('teacher.json')
type_teacher.loc["status"] = "teacher"
type_teacher.to_json("testTeacher.json")
with open("testStudent.json") as data_file:
data_student = json.load(data_file)
with open("testTeacher.json") as data_file:
data_teacher = json.load(data_file)
我想做的是将data_student和data_teacher合并,并将结果JSON打印到一个JSON文件中,但我只能使用标准库、pandas、numpy和scipy。
经过一些测试,我意识到有些老师也是学生,这可能会对合并造成问题。