Python-如何将数据文件.json与日志文件.json进行比较,并输出包含更改的新文件.json



我在json中有一个员工文件(empid,name,title(,在json上有一个每周更改日志(empid、name、title(,我需要将这两个字段进行比较,并将字段更改为employee,然后用json写一个新文件。

以下是我所拥有的://应用程序使用传入的.json文件和一个更改文件来执行更新并输出一个新.json文件//传入文件被读取到python字典中,更改文件也被读取到python字典中//变更文件具有1个附加字段"0";recttype";这用于确定为该更改记录执行的操作——应用于输入文件的该文件条目;更新、附加或删除

# 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

通过一种非常简单的方式,您可以将这两个文件转换为python字典,然后对其进行处理

最新更新