【发布时间】:2020-11-02 06:31:10
【问题描述】:
我在 json 中有一个员工文件(empid、name、title),在 json 中有一个每周更改日志(empid、name、title),我需要比较两者并将字段更改为员工并在其中写入一个新文件json.
这是我所拥有的: //App 使用传入的 .json 文件和一个更改文件来执行更新并输出一个新的 .json 文件 //传入的文件读入python字典,change文件也读入python字典 //更改文件有 1 个附加字段“rectype”;这用于确定为该更改记录执行的操作 - 应用于输入文件的该文件条目;更新、追加或删除
# importing the module
import json
# Opening input JSON file into dictionary "inp"
with open('path/inputfilename.json') as input_file:
inp = json.load(input_file)
#for dev visibility show data types
print("Type:", type(inp))
# Opening change JSON file into dictionary "chg"
with open('path/change_file.json') as change_file:
chg = json.load(change_file)
#for dev visibility show data types
print("Type:", type(chg))
#loop through through change file
for c in chg
if chg{rectype(c)}='update'
then inp.update({"color": "White"})
fromkeys()
break
if chg{rectype(c)}='delete'
#delete record in inp
break
if chg{rectype(c)}='append'
#add record to inp
break
【问题讨论】:
-
欢迎来到 SO!你能展示一下你到目前为止所做的尝试吗?此外,提供 MRE 可以更高效地创建答案,并且还可以让您进一步了解您的问题 (How to create a Minimal, Reproducible Example)。