[英]Python- How to compare a data file .json to a log file .json and output a new file .json with changes incorporated
我在json中有一個員工文件(empid,name,title)和json中的每周更改日志(empid,name,title),我需要比較兩者並將字段更改為employee並在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
以一種非常簡單的方式,您可以將這兩個文件轉換為 python 字典,然后處理它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.