繁体   English   中英

Python脚本用于过滤包含JSON对象中特定值的数组

[英]Python Script to filter arrays containing a specific value in JSON object

我有一个json对象,它由一个带有键'data'的对象组成,它有一组数组中列出的值。 我需要返回包含值x的所有数组,但数组本身没有键。 我正在尝试编写一个脚本来输入源文件(inFile)和一个定义导出文件(outFile)。 这是我的数据结构:

{ "data": [
       ["x", 1, 4, 6, 2, 7],
       ["y", 3, 2, 5, 8, 4],
       ["z", 5, 2, 5, 9, 9],
       ["x", 3, 7, 2, 6, 8]
     ]
}

这是我目前的脚本:

import json

def jsonFilter( inFile, outFile ):
    out = None;

    with open( inFile, 'r') as jsonFile:
       d = json.loads(json_data)
       a = d['data']
       b = [b for b in a if b != 'x' ]
       del b
       out = a


    if out:
        with open( outFile, 'w' ) as jsonFile:
            jsonFile.write( json.dumps( out ) );

    else:
       print "Error creating new jsonFile!"

感谢Rob和大家的帮助! 这是最终的工作命令行工具。 这需要两个参数:inFile和Outfile。 〜$ python jsonFilter.py inFile.json outFile.json

import json

def jsonFilter( inFile, outFile ):
    # make a dictionary.
    out = {};

    with open( inFile, 'r') as jsonFile:
       json_data = jsonFile.read()
       d = json.loads(json_data)
       # build the data you want to save to look like the original
       # by taking the data in the d['data'] element filtering what you want
       # elements where b[0] is 'x'
       out['data'] = [b for b in d['data'] if b[0] == 'x' ]


    if out:
        with open( outFile, 'w' ) as jsonFile:
            jsonFile.write( json.dumps( out ) );

    else:
       print "Error creating new JSON file!"

if __name__ == "__main__":
     import argparse

     parser = argparse.ArgumentParser()
     parser.add_argument('inFile', nargs=1, help="Choose the in file to use")
     parser.add_argument('outFile', nargs=1, help="Choose the out file to use")
     args = parser.parse_args()
     jsonFilter( args.inFile[0] , args.outFile[0] );

第一个问题查询字符串对于所有内容都是真的(也就是说,因为你将b(列表)与'x'比较为字符串,所以返回整个数据集

  b = [b for b in a if b != 'x' ]

你想做的是:

  b = [b for b in a if b[0] != 'x' ]

第二个问题是您尝试通过查询和删除结果来删除数据。 由于结果包含不会从原始容器中删除任何内容的副本。
而是仅使用您想要的元素构建新数据,并保存它们。 此外,您没有在out数据中重新创建'data'元素,因此json使输出具有与输入数据相同的结构。

import json

def jsonFilter( inFile, outFile ):
    # make a dictionary instead.
    out = {};

    with open( inFile, 'r') as jsonFile:
       json_data = jsonFile.read()
       d = json.loads(json_data)
       # build the data you want to save to look like the original
       # by taking the data in the d['data'] element filtering what you want
       # elements where b[0] is 'x'
       out['data'] = [b for b in d['data'] if b[0] == 'x' ]


    if out:
        with open( outFile, 'w' ) as jsonFile:
            jsonFile.write( json.dumps( out ) );

    else:
       print "Error creating new jsonFile!"

输出json数据看起来像:

 '{"data": [["x", 1, 4, 6, 2, 7], ["x", 3, 7, 2, 6, 8]]}'

如果您不希望输出具有'data'根元素,而只需要与您的过滤器匹配的数据数组,则更改该行:

 out['data'] = [b for b in d['data'] if b[0] == 'x' ]

 out = [b for b in d['data'] if b[0] == 'x' ]

通过此更改,输出json数据如下所示:

 '[["x", 1, 4, 6, 2, 7], ["x", 3, 7, 2, 6, 8]]'

所以,基本上你想要过滤掉包含第一个元素是'x'的数组的输入数据,可能这样做:

import json


def jsonFilter(inFile, outFile):
    with open(inFile, 'r') as jsonFile:
        d = json.loads(json_data)

        out = {
            'data': filter(lambda x: x[0] == 'x', d['data'])
        }

        if out['data']:
            with open(outFile, 'w') as jsonFile:
                jsonFile.write(json.dumps(out))
        else:
            print "Error creating new jsonFile!"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM