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

时间:2016-08-26 17:40:08

标签: python arrays json

我有一个json对象,它由一个带有键'数据'的对象组成,它具有一组数组中列出的值。我需要返回包含值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] );

2 个答案:

答案 0 :(得分:2)

第一个问题查询字符串对于所有内容都是真的(也就是说,因为你要将b(列表)与' x'一个字符串

进行比较而返回整个数据集
  b = [b for b in a if b != 'x' ]

你想做的是:

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

第二个问题是您尝试通过查询和删除结果来删除数据。由于结果包含不会从原始容器中删除任何内容的副本。
而是仅使用您想要的元素构建新数据,并保存它们。此外,您没有重新创建数据' out数据中的元素,因此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]]}'

如果您不希望输出包含“数据”。根元素,但只是与您的过滤器匹配的数据数组,然后更改行:

 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]]'

答案 1 :(得分:1)

所以,基本上你想要过滤掉包含第一个元素是'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!"