【问题标题】:Python- How to compare a data file .json to a log file .json and output a new file .json with changes incorporatedPython-如何将数据文件 .json 与日志文件 .json 进行比较并输出包含更改的新文件 .json
【发布时间】: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)。

标签: python json


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2021-11-17
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多