Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我有json文件当json文件被更改时,我需要在python控制台中显示更改_Json_Python 3.x - Fatal编程技术网

我有json文件当json文件被更改时,我需要在python控制台中显示更改

我有json文件当json文件被更改时,我需要在python控制台中显示更改,json,python-3.x,Json,Python 3.x,我有本地json文件,我是python的初学者,我需要为运行python代码开发一个代码,它会不断检查json文件,如果json文件中有任何更改,我需要在python控制台中显示更改 例如。, { “a”:2, “b”:3 } 如果我更改“a”:3 python输出: 在键a和值3处检测到更改 我在这里缺乏逻辑。提前谢谢你,你好,看来你有个项目要做。根据您想要更改的值以及它们在JSON中的深度,可以为任务添加更多的工作。让我看看我能想出什么… import essentials # pip i

我有本地json文件,我是python的初学者,我需要为运行python代码开发一个代码,它会不断检查json文件,如果json文件中有任何更改,我需要在python控制台中显示更改

例如。, { “a”:2, “b”:3 } 如果我更改“a”:3

python输出:

在键a和值3处检测到更改


我在这里缺乏逻辑。提前谢谢你,你好,看来你有个项目要做。根据您想要更改的值以及它们在JSON中的深度,可以为任务添加更多的工作。让我看看我能想出什么…

import essentials #  pip install mknxgn_essentials (not gonna lie, i made this module. biased...)
import time
import os
recordedjson = essentials.EsFileObject("Tools/Json.json").json
recoredchanges = essentials.EsFileObject("Tools/Changes.json")
if recoredchanges.json == False: # Used to save changes, also makes sure to keep previous logs
    recoredchanges.setjson([])
onchange_record_new_json = True # makes the new json the one you want to compare after each change
while True:
    newjson = essentials.EsFileObject('Tools/Json.json').json
    if recordedjson != newjson:
        changes = 0
        keychanges = 0
        newkeys = 0
        removedkeys = 0
        valuechanges = 0
        print("File has been changed! - Collecting Changes")
        for obj in recordedjson:
            if obj not in newjson:
                print("Key Removed From New Json Key:", obj)
                keychanges += 1
                removedkeys += 1
                changes += 1
            else:
                if recordedjson[obj] != newjson[obj]:
                    print("Value Change!")
                    print("Previous- Key: ", obj)
                    print("@ Value:", recordedjson[obj])
                    print(" New- Value:", newjson[obj])
                    valuechanges += 1
                    changes += 1
        for obj in newjson:
            if obj not in recordedjson:
                if obj not in newjson:
                    print("New Key Introduced- Key:", obj)
                    keychanges += 1
                    removedkeys += 1
                    changes += 1
        changelog = "Change Count: " + str(changes)
        changelog += " Key Changes: " + str(keychanges)
        changelog += " New Keys: " + str(newkeys)
        changelog += " Removed Keys: " + str(removedkeys)
        changelog += " Value Changes: " + str(valuechanges)
        print(changelog)
        record = {"Change Time": essentials.EsTimeObject().string}
        record["User Readable Time Change"] = essentials.EsTimeObject().readable
        record["Change Log"] = changelog
        recoredchanges.json.append(record)
        recoredchanges.save()
        time.sleep(10)
        if onchange_record_new_json:
            recordedjson = newjson
    print("Waiting For Change")
    time.sleep(1)
    os.system("cls") # cls for windows, clear for linux and so on...
在这个网站上发布代码真烦人