首页 > 解决方案 > Python-如何将数据文件 .json 与日志文件 .json 进行比较并输出包含更改的新文件 .json

问题描述

我在 json 中有一个员工文件(empid、name、title),在 json 中有一个每周更改日志(empid、name、title),我需要比较两者并将字段更改为员工并在 json 中编写一个新文件。

这是我所拥有的: //App 使用传入的 .json 文件和更改文件来执行更新并输出新的 .json 文件 //传入文件被读入 python 字典,更改文件也读入 python 字典 //change文件有 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

标签: pythonjson

解决方案


以一种非常简单的方式,您可以将这两个文件转换为 python 字典,然后处理它们


推荐阅读