[英]Calculate the percentage difference between two specific rows in Python pandas
[英]Python; calculate the percentage of difference between two json
我有一些本地 json 文件。 例如:
JSON 1
{
"events":{
"id1":{
"name":"Marcus",
"surname":"Redwhite",
"age":"22",
"text":{
"description":"Some description ...",
"title":"title of description"
}
},
"id2":{
"name":"Fred",
"surname":"Rose",
"age":"30",
"text":{
"description":"Some description ...",
"title":"title of description"
}
}
}
JSON 1 修改
{
"events":{
"id1":{
"name":"Marcus Modified",
"surname":"Redwhite Modified",
"age":"22",
"text":{
"description":"Some description ...",
"title":"title of description Modified"
}
},
"id2":{
"name":"Fred",
"surname":"Rose Modified",
"age":"50",
"text":{
"description":"Some description ... Modified",
"title":"title of description"
}
}
}
我必須比較這些 Json 文件(在此示例中,名稱字段、姓字段、年齡字段和文本字段已修改)並且我必須計算它們之間的差異百分比(繪制餅圖或任何其他圖表)。 有沒有辦法做到這一點?
import json
import glob
# get list of All json files in different folders:
originalJsonFilesList = glob.glob("C:/Python/OriginalJson/*.json")
modifiedJsonFilesList = glob.glob("C:/Python/ModifiedJson/*.json")
# Loop all list
for originalfile, modifiedFile in originalJsonFilesList, modifiedJsonFilesList:
# Opening JSON files (original and modified)
originalJson = open(originalfile)
modifiedJson = open(modifiedFile)
# load as dictionary
data1 = json.load(originalJson)
data2 = json.load(modifiedJson)
#############################################
# Something for calculateing difference
# of percentage between data1 and data2
############################################
# Closing files
originalJson.close()
modifiedJson.close()
第一的:
將您的 json 轉換為 dict
dict_json1 = json.loads(json_1)
dict_json_modified = json.loads(json_modified)
第二:
將它們轉換為設置:
dict_json1 = set(dict_json1.items())
dict_json_modified = set(dict_json_modified.items())
差異 = dict_json1 ^ dict_json_modified
打印(差異)
您需要實現一種稱為Levenshtein Distance Metric的算法
還有一個問題與你的情況類似,有另一種解決方案,你可以看看。
您還可以從difflib檢查 SequenceMatcher
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.