合并/组合/附加2个没有公共值/数据的JSON文件(在Python中)

时间:2018-03-12 09:32:49

标签: python json dictionary merge

我一直在尝试合并两个没有公共数据的JSON文件.1文件是嵌套字典列表,而另一个只有一个字典(文件2中没有嵌套)。 我尝试使用引入一个新的键值对['tmp'] = 1,所以我可以合并该键。但是代码正在运行并以代码137状态退出。(sigkill)。 文件2的数据必须在文件1中的数据中附加或合并,但缺少通用参数没有帮助。我尝试将数据合并为python列表以及使用pandas的DataFrame。

import json import pandas as pd from pandas import DataFrame

def df_crossjoin(df, df1, **kwargs):
    df['_tmpkey'] = 1
    df1['_tmpkey'] = 1

    res = pd.merge(df, df1, on='_tmpkey', **kwargs).drop('_tmpkey', axis=1)    
    res.index = pd.MultiIndex.from_product((df.index,df1.index))    
    df.drop('_tmpkey', axis=1, inplace=True)   
    df1.drop('_tmpkey', axis=1, inplace=True)

    return res    
    with open("data.json",encoding='utf-8') asinput_file:
       with open("IPAMData.json", encoding='utf-8') as input_file1:
            data = json.load(input_file)
            df = pd.DataFrame(data)

            data1 = json.load(input_file1)
            df1=pd.DataFrame(data1)
            df_cartesian=df_crossjoin(df,df1,suffixes=('_left','_right'))
            df_cartesian.to_json("FILE11.json",orient='records', lines=True)

文件中的数据是:File 1-data.json:

 [ { "_ref": "network/ZAvMjIvMA:192.168.15.0/22/default", "comment": "ABS", "dhcp_utilization": 0, "disable": false, "enable_ddns": false, "extattrs": { "CITY": { "value": "CITY" }, "COUNTRY": { "value": "COUN" }, "Helpnow ID": { "value": "TA04" }, "OWNER": { "value": "owner@exam.com" }, "REGION": { "value": "REG" }, "SITE_CODE": { "value": "E1" }, "VLAN Type": { "value": "TYPE" }, "VLAN ID": { "value": 2 }, "VLAN DESC": { "value": "data value" }, "synchronized_with_presto": { "value": 0 } }, "members": [], "network": "192.168.15.0/22" }, { "_ref": "network/Z4gyLzA:10.4.7.0/24/default", "comment": "comment", "dhcp_utilization": 0, "disable": false, "enable_ddns": false,"extattrs": { "CITY": { "value": "ABC" }, "COUNTRY": { "value": "COUNTRY" }, "Helpnow ID": { "value": "O1" }, "OWNER": { "value": "cyz@example.com" "REGION": { "value": "REGION" }, "SITE_CODE": { "value": "T1" }, "VLAN Type": { "value": "TN" }, "VLAN ID": { "value": 1612 }, "VLAN DESC": { "value": "DESC" }, "synchronized_with_presto": { "value": 0 } }, "members": [], "network": "10.4.7.0/22" },....]

file 2 IPAMData.json:

[ { "_ref": "ipam:statistics/ZAujjMjIvMA:default/192.168.15.0/22", "network": "192.168.15.0", "utilization": 640 }, { "_ref": "ipam:statistics/ZG5AuIyLzA:default/10.4.7.0/22", "network": "10.4.7.0", "utilization": 9 }, ... ]

输出:处理以退出代码137结束(由信号9中断:SIGKILL)

预期输出可以是这样的:

 [ { "_ref": "network/ZAvMjIvMA:192.168.15.0/22/default", "comment": "ABS","_ref": "ipam:statistics/ZAMjIvMA:default/192.168.15.0/22", "network": "192.168.15.0", "utilization": 640, "dhcp_utilization": 0, "disable": false, "enable_ddns": false,  "extattrs": { "CITY": { "value": "CITY" }, "COUNTRY": { "value": "COUN" }, "Helpnow ID": { "value": "TA04" }, "OWNER": { "value": "owner@exam.com" }, "REGION": { "value": "REG" }, "SITE_CODE": { "value": "E1" }, "VLAN Type": { "value": "TYPE" }, "VLAN ID": { "value": 2 }, "VLAN DESC": { "value": "data value" }, "synchronized_with_presto": { "value": 0 } }, "members": [], "network": "192.168.15.0/22" } { "_ref": "network/Z4gyLzA:10.4.7.0/24/default", "comment": "comment","_ref": "ipam:statistics/ZG5AuIyLzA:default/10.4.7.0/22", "network": "10.4.7.0", "utilization": 9 , "dhcp_utilization": 0, "disable": false, "enable_ddns": false, "extattrs": { "CITY": { "value": "ABC" }, "COUNTRY": { "value": "COUNTRY" }, "Helpnow ID": { "value": "O1" }, "OWNER": { "value": "cyz@example.com" "REGION": { "value": "REGION" }, "SITE_CODE": { "value": "T1" }, "VLAN Type": { "value": "TN" }, "VLAN ID": { "value": 1612 }, "VLAN DESC": { "value": "DESC" }, "synchronized_with_presto": { "value": 0 } }, "members": [], "network": "10.4.7.0/22" },....]

文件2中的数据必须附加到文件1中的数据,其中文件2中的每个字典对应于文件1中的字典,顺序相同。

0 个答案:

没有答案