Python 查找json输出的差异

Python 查找json输出的差异,python,json,xml,csv,Python,Json,Xml,Csv,我有两个不同格式的文件,即(.csv和.xml)。我已经用python编写了从这两个文件中获取ip地址、Qid和端口的程序。我已将json格式的输出重定向到一个文件。我需要帮助找到json输出中的差异,将2个ip地址作为输入,并找到2个ip地址之间qid和端口的相似性和差异。这是我的密码 #! python import xml.etree.ElementTree as ET import sys import json import csv import os def CSV(file):

我有两个不同格式的文件,即(.csv和.xml)。我已经用python编写了从这两个文件中获取ip地址、Qid和端口的程序。我已将json格式的输出重定向到一个文件。我需要帮助找到json输出中的差异,将2个ip地址作为输入,并找到2个ip地址之间qid和端口的相似性和差异。这是我的密码

#! python
import xml.etree.ElementTree as ET
import sys
import json
import csv
import os

def CSV(file):
    os.chdir('C:\\Users\\sraina\\Desktop\\tool_project')
    scan = file
    f1 = open(scan)
    csvReader = csv.reader(f1)
    csvData = list(csvReader)
    length = len(csvData)
    for i in range(8,length-1):
        try:
            a = csvData[i][0]
            b = csvData[i][5]
            c = csvData[i][9]
            Nfile = open('jscan','a')
            dump = {u"IP":a, u"QID": b, u"Port": c}
            d =  (json.dumps(dump, indent= 4))
            Nfile.write(d)
            Nfile.close()

        except:
            print  "\nThe row was empty or something went wrong while reading data from CSV..."

    f1.close
def xml(file):
    os.chdir('C:\\Users\\sraina\\Desktop\\tool_project')
    scan = file
    f = open(scan)
    tree = ET.parse(f)
    for node in tree.iter('*'):
        if node in tree.iter('IP'):
            a = "IP is "+ str(node.attrib.get("value"))
        if node in tree.iter('VULNS'):
            x = "********Vulns*********"
        if node in tree.iter('CAT'):
            b = "Port is " + str(node.attrib.get("port"))
        if node in tree.iter('VULN'):
            c = "Vuln Qid is " + str(node.attrib.get("number"))
            Nfile = open('jscan','a')
            dump = {u"V": x,u"IP": a, u"Port": b, u"Qid": c}
            d1 = (json.dumps(dump, indent=4))
            Nfile.write(d1)
            Nfile.close()
        if node in tree.iter('INFOS'):
            x1 = "********Infos*********"
        if node in tree.iter('CAT'):
            b1 = "Port is " + str(node.attrib.get("port"))
        if node in tree.iter('INFO'):
            c1 = "Info Qid is "+ str(node.attrib.get("number"))
            Nfile = open('jscan','a')
            dump = {u"IG": x1,u"IP": a, u"Port": b1, u"Qid": c1}
            d2 = (json.dumps(dump, indent=4))
            Nfile.write(d2)
            Nfile.close()

        if node in tree.iter('PRACTICES'):
            x2 = "********Practice************"
        if node in tree.iter('CAT'):
            b2 = "Port is " + str(node.attrib.get("port"))
        if node in tree.iter('PRACTICE'):
            c2 = "Practice Qid is "+ str(node.attrib.get("number"))
            Nfile = open('jscan','a')
            dump = {u"P": x2,u"IP": a, u"Port": b2, u"Qid": c2}
            d3 = (json.dumps(dump, indent=4))
            Nfile.write(d3)
            Nfile.close()
    f.close()

def JSON(file):
    for i in range(5):
        print "Enter Your file"
        file = raw_input()
        if os.path.isfile(file):
            if file.endswith('.csv'):
                CSV(file)
            elif file.endswith('.xml'):
                 xml(file)
            else:
                  print "file should be in xml or csv format"

        elif  file == "e":
            sys.exit()
        else:
            print "maybe file doesnt exists"




def main():
    os.chdir('C:\\Users\\sraina\\Desktop\\tool_project')
    file = ('new')
    JSON(file)

if __name__ == '__main__':
    main()
JSON输出如下所示:

 {
    "IP": "10.10.36.45", 
    "Port": "1521", 
    "QID": "20046"
}{
    "IP": "10.10.36.45", 
    "Port": "", 
    "QID": "19129"
}{
    "IP": "10.10.36.45", 
    "Port": "", 
    "QID": "19226"
}{
    "IP": "10.10.36.45", 
    "Port": "1521", 
    "QID": "19221"
}{
    "IP": "10.10.35.85", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.35.19", 
    "Port": "", 
    "QID": "236554"
}{
    "IP": "10.10.34.146", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.34.131", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.33.182", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.33.172", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.33.137", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.31.99", 
    "Port": "", 
    "QID": "236554"
}{
    "IP": "10.10.31.97", 
    "Port": "", 
    "QID": "236554"
}{
    "IP": "10.10.31.129", 
    "Port": "", 
    "QID": "236554"
}{
    "IP": "10.10.25.38", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.24.85", 
    "Port": "", 
    "QID": "370658"
}{
    "IP": "10.10.24.182", 
    "Port": "", 
    "QID": "370658"
}

您的json输出不是有效的json,您可以在线检查如何更正json并将其重新粘贴。这是我在将其重定向到文件时获得的输出。输出中的无效内容以及如何更正它。请提出建议。如果您将json输出粘贴到此处并进行验证,您会发现这不是有效的json,因此不会使用json模块对其进行解析。您有两个选项1)以有效的json格式写入输出,您需要更改编写输出的代码2)将输出文件视为普通文件,并查看是否可以创建一些嵌套字典来解析数据您的json输出不是有效的json,您可以在线检查如何更正json并将其重新粘贴这是我在将其重定向到文件时获得的输出。输出中的无效内容以及如何更正它。请提出建议。如果您将json输出粘贴到此处并进行验证,您会发现这不是有效的json,因此不会使用json模块对其进行解析。您有两个选项1)以有效的json格式写入输出,您需要修改编写输出的代码2)将输出文件视为普通文件,并查看是否可以创建一些嵌套字典来解析数据