在python中过滤JSON数据

时间:2018-04-07 01:18:03

标签: python arrays json

我有一个包含5000个项目的电影演员的 CSV JSON文件。第一项看起来像这样:

[{
    "cast_id": 5,
    "character": "John Carter",
    "credit_id": "52fe479ac3a36847f813ea75",
    "gender": 2,
    "id": 60900,
    "name": "Taylor Kitsch",
    "order": 0
}, {
    "cast_id": 20,
    "character": "Dejah Thoris",
    "credit_id": "52fe479ac3a36847f813eab3",
    "gender": 1,
    "id": 21044,
    "name": "Lynn Collins",
    "order": 1
}, {
    "cast_id": 7,
    "character": "Sola",
    "credit_id": "52fe479ac3a36847f813ea79",
    "gender": 1,
    "id": 2206,
    "name": "Samantha Morton",
    "order": 2
}, {
    "cast_id": 3,
    "character": "Tars Tarkas",
    "credit_id": "52fe479ac3a36847f813ea6d",
    "gender": 2,
    "id": 5293,
    "name": "Willem Dafoe",
    "order": 3
}, {
    "cast_id": 8,
    "character": "Tal Hajus",
    "credit_id": "52fe479ac3a36847f813ea7d",
    "gender": 2,
    "id": 19159,
    "name": "Thomas Haden Church",
    "order": 4
}, {
    "cast_id": 2,
    "character": "Matai Shang",
    "credit_id": "52fe479ac3a36847f813ea69",
    "gender": 2,
    "id": 2983,
    "name": "Mark Strong",
    "order": 5
}, {
    "cast_id": 4,
    "character": "Tardos Mors",
    "credit_id": "52fe479ac3a36847f813ea71",
    "gender": 2,
    "id": 8785,
    "name": "Ciar\u00e1n Hinds",
    "order": 6
}, {
    "cast_id": 9,
    "character": "Sab Than",
    "credit_id": "52fe479ac3a36847f813ea81",
    "gender": 2,
    "id": 17287,
    "name": "Dominic West",
    "order": 7
}, {
    "cast_id": 10,
    "character": "Kantos Kan",
    "credit_id": "52fe479ac3a36847f813ea85",
    "gender": 2,
    "id": 17648,
    "name": "James Purefoy",
    "order": 8
}, {
    "cast_id": 11,
    "character": "Powell",
    "credit_id": "52fe479ac3a36847f813ea89",
    "gender": 2,
    "id": 17419,
    "name": "Bryan Cranston",
    "order": 9
}, {
    "cast_id": 12,
    "character": "Sarkoja",
    "credit_id": "52fe479ac3a36847f813ea8d",
    "gender": 1,
    "id": 6416,
    "name": "Polly Walker",
    "order": 10
}, {
    "cast_id": 13,
    "character": "Edgar Rice Burroughs",
    "credit_id": "52fe479ac3a36847f813ea91",
    "gender": 2,
    "id": 57675,
    "name": "Daryl Sabara",
    "order": 11
}, {
    "cast_id": 14,
    "character": "Stayman #1 / Helm",
    "credit_id": "52fe479ac3a36847f813ea95",
    "gender": 2,
    "id": 89830,
    "name": "Arkie Reece",
    "order": 12
}, {
    "cast_id": 15,
    "character": "Stayman #3",
    "credit_id": "52fe479ac3a36847f813ea99",
    "gender": 2,
    "id": 205278,
    "name": "Davood Ghadami",
    "order": 13
}, {
    "cast_id": 16,
    "character": "Lightmaster",
    "credit_id": "52fe479ac3a36847f813ea9d",
    "gender": 1,
    "id": 218345,
    "name": "Pippa Nixon",
    "order": 14
}, {
    "cast_id": 46,
    "character": "Thern #2",
    "credit_id": "584ef986c3a3682a940010d0",
    "gender": 2,
    "id": 1390394,
    "name": "James Embree",
    "order": 15
}, {
    "cast_id": 77,
    "character": "Thern #1",
    "credit_id": "58c68f82c3a3684114014f58",
    "gender": 0,
    "id": 1518112,
    "name": "Philip Philmar",
    "order": 16
}, {
    "cast_id": 47,
    "character": "Pretty Woman in NYC Doorway",
    "credit_id": "584f133992514107110024b8",
    "gender": 1,
    "id": 1721985,
    "name": "Emily Tierney",
    "order": 17
}, {
    "cast_id": 48,
    "character": "Telegraph Clerk",
    "credit_id": "584f16d192514107000026a2",
    "gender": 2,
    "id": 1721992,
    "name": "Edmund Kente",
    "order": 18
}, {
    "cast_id": 49,
    "character": "Dalton",
    "credit_id": "584f1a94c3a3682a8d0026e7",
    "gender": 2,
    "id": 118617,
    "name": "Nicholas Woodeson",
    "order": 19
}, {
    "cast_id": 50,
    "character": "Stable Boy",
    "credit_id": "584f1f2b9251410700002be9",
    "gender": 2,
    "id": 1722006,
    "name": "Kyle Agnew",
    "order": 20
}, {
    "cast_id": 51,
    "character": "Dix the Storekeeper",
    "credit_id": "584f28aec3a3683150000214",
    "gender": 2,
    "id": 130129,
    "name": "Don Stark",
    "order": 21
}, {
    "cast_id": 52,
    "character": "Rowdy #1",
    "credit_id": "58580465c3a3683150056d0c",
    "gender": 2,
    "id": 65716,
    "name": "Josh Daugherty",
    "order": 22
}, {
    "cast_id": 53,
    "character": "Rowdy #2",
    "credit_id": "58580cd89251411a4605f517",
    "gender": 2,
    "id": 1724736,
    "name": "Jared Cyr",
    "order": 23
}, {
    "cast_id": 37,
    "character": "Stockade Guard",
    "credit_id": "54e5a58d925141529c000f89",
    "gender": 2,
    "id": 62082,
    "name": "Christopher Goodman",
    "order": 24
}, {
    "cast_id": 54,
    "character": "Sarah Carter",
    "credit_id": "585823dc925141594100c816",
    "gender": 1,
    "id": 1367241,
    "name": "Amanda Clayton",
    "order": 25
}, {
    "cast_id": 170,
    "character": "Apache #1 (as Joe Billingiere)",
    "credit_id": "595ad40c9251410bfa04831e",
    "gender": 0,
    "id": 1844319,
    "name": "Joseph Billingiere",
    "order": 26
}
]

我只需要此文件中的“name”值。例如,在这个项目中它将是:

Taylor Kitsch, Lynn Collins, Samantha Morton, Willem Dafoe, Thomas Haden Church, Mark Strong, Ciar Hinds, Dominic West, James Purefoy, Bryan Cranston, Polly Walker

使用键作为“名称”来表示值。

我应该怎么做呢?

5 个答案:

答案 0 :(得分:2)

这样的事情应该有效。您可以使用csv库将行转换为数组以进行迭代。 ast库会将一个字符串转换为您可以键入的dict / json对象。

import ast
import csv
names = []

with open('csvFile.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        names.append(ast.literal_eval(row)[name])
        # or you could print(name) here. 

答案 1 :(得分:2)

您展示的是单个 JSON数组,而不是CSV。 (文件扩展名与Python无关)

从每个JSON数组行中的对象中解析名称

import json

with open("file.txt") as f:
    for line in f:
        names = (x['name'] for x in json.loads(line))
        for name in names:
            print(name) 

答案 2 :(得分:0)

根据您提供的内容,这是一个非常基本的例子:

>>> movie =  [{"cast_id": 5, "character": "John Carter", "credit_id": "52fe479ac3a36847f813ea75", "gender": 2, "id": 60900, "name": "Taylor Kitsch", "order": 0}, {"cast_id": 20, "character": "Dejah Thoris", "credit_id": "52fe479ac3a36847f813eab3", "gender": 1, "id": 21044, "name": "Lynn Collins", "order": 1}]
>>> for i in movie:
...     print(i["name"])
...
Taylor Kitsch
Lynn Collins

基本上,这会迭代列表中的不同字典,然后提取与"name"键关联的值。

在整个csv文件中执行此操作需要更多信息,但这可能会让您入门。

答案 3 :(得分:0)

您可以直接使用过滤器发出请求

{'$filter': ['name eq Lynn Collins']}

答案 4 :(得分:-1)

my_list = [{
    "cast_id": 5,
    "character": "John Carter",
    "credit_id": "52fe479ac3a36847f813ea75",
    "gender": 2,
    "id": 60900,
    "name": "Taylor Kitsch",
    "order": 0
}, {
    "cast_id": 20,
    "character": "Dejah Thoris",
    "credit_id": "52fe479ac3a36847f813eab3",
    "gender": 1,
    "id": 21044,
    "name": "Lynn Collins",
    "order": 1
}, {
    "cast_id": 7,
    "character": "Sola",
    "credit_id": "52fe479ac3a36847f813ea79",
    "gender": 1,
    "id": 2206,
    "name": "Samantha Morton",
    "order": 2
}, {
    "cast_id": 3,
    "character": "Tars Tarkas",
    "credit_id": "52fe479ac3a36847f813ea6d",
    "gender": 2,
    "id": 5293,
    "name": "Willem Dafoe",
    "order": 3
}, {
    "cast_id": 8,
    "character": "Tal Hajus",
    "credit_id": "52fe479ac3a36847f813ea7d",
    "gender": 2,
    "id": 19159,
    "name": "Thomas Haden Church",
    "order": 4
}, {
    "cast_id": 2,
    "character": "Matai Shang",
    "credit_id": "52fe479ac3a36847f813ea69",
    "gender": 2,
    "id": 2983,
    "name": "Mark Strong",
    "order": 5
}, {
    "cast_id": 4,
    "character": "Tardos Mors",
    "credit_id": "52fe479ac3a36847f813ea71",
    "gender": 2,
    "id": 8785,
    "name": "Ciar\u00e1n Hinds",
    "order": 6
}, {
    "cast_id": 9,
    "character": "Sab Than",
    "credit_id": "52fe479ac3a36847f813ea81",
    "gender": 2,
    "id": 17287,
    "name": "Dominic West",
    "order": 7
}, {
    "cast_id": 10,
    "character": "Kantos Kan",
    "credit_id": "52fe479ac3a36847f813ea85",
    "gender": 2,
    "id": 17648,
    "name": "James Purefoy",
    "order": 8
}, {
    "cast_id": 11,
    "character": "Powell",
    "credit_id": "52fe479ac3a36847f813ea89",
    "gender": 2,
    "id": 17419,
    "name": "Bryan Cranston",
    "order": 9
}, {
    "cast_id": 12,
    "character": "Sarkoja",
    "credit_id": "52fe479ac3a36847f813ea8d",
    "gender": 1,
    "id": 6416,
    "name": "Polly Walker",
    "order": 10
}, {
    "cast_id": 13,
    "character": "Edgar Rice Burroughs",
    "credit_id": "52fe479ac3a36847f813ea91",
    "gender": 2,
    "id": 57675,
    "name": "Daryl Sabara",
    "order": 11
}, {
    "cast_id": 14,
    "character": "Stayman #1 / Helm",
    "credit_id": "52fe479ac3a36847f813ea95",
    "gender": 2,
    "id": 89830,
    "name": "Arkie Reece",
    "order": 12
}, {
    "cast_id": 15,
    "character": "Stayman #3",
    "credit_id": "52fe479ac3a36847f813ea99",
    "gender": 2,
    "id": 205278,
    "name": "Davood Ghadami",
    "order": 13
}, {
    "cast_id": 16,
    "character": "Lightmaster",
    "credit_id": "52fe479ac3a36847f813ea9d",
    "gender": 1,
    "id": 218345,
    "name": "Pippa Nixon",
    "order": 14
}, {
    "cast_id": 46,
    "character": "Thern #2",
    "credit_id": "584ef986c3a3682a940010d0",
    "gender": 2,
    "id": 1390394,
    "name": "James Embree",
    "order": 15
}, {
    "cast_id": 77,
    "character": "Thern #1",
    "credit_id": "58c68f82c3a3684114014f58",
    "gender": 0,
    "id": 1518112,
    "name": "Philip Philmar",
    "order": 16
}, {
    "cast_id": 47,
    "character": "Pretty Woman in NYC Doorway",
    "credit_id": "584f133992514107110024b8",
    "gender": 1,
    "id": 1721985,
    "name": "Emily Tierney",
    "order": 17
}, {
    "cast_id": 48,
    "character": "Telegraph Clerk",
    "credit_id": "584f16d192514107000026a2",
    "gender": 2,
    "id": 1721992,
    "name": "Edmund Kente",
    "order": 18
}, {
    "cast_id": 49,
    "character": "Dalton",
    "credit_id": "584f1a94c3a3682a8d0026e7",
    "gender": 2,
    "id": 118617,
    "name": "Nicholas Woodeson",
    "order": 19
}, {
    "cast_id": 50,
    "character": "Stable Boy",
    "credit_id": "584f1f2b9251410700002be9",
    "gender": 2,
    "id": 1722006,
    "name": "Kyle Agnew",
    "order": 20
}, {
    "cast_id": 51,
    "character": "Dix the Storekeeper",
    "credit_id": "584f28aec3a3683150000214",
    "gender": 2,
    "id": 130129,
    "name": "Don Stark",
    "order": 21
}, {
    "cast_id": 52,
    "character": "Rowdy #1",
    "credit_id": "58580465c3a3683150056d0c",
    "gender": 2,
    "id": 65716,
    "name": "Josh Daugherty",
    "order": 22
}, {
    "cast_id": 53,
    "character": "Rowdy #2",
    "credit_id": "58580cd89251411a4605f517",
    "gender": 2,
    "id": 1724736,
    "name": "Jared Cyr",
    "order": 23
}, {
    "cast_id": 37,
    "character": "Stockade Guard",
    "credit_id": "54e5a58d925141529c000f89",
    "gender": 2,
    "id": 62082,
    "name": "Christopher Goodman",
    "order": 24
}, {
    "cast_id": 54,
    "character": "Sarah Carter",
    "credit_id": "585823dc925141594100c816",
    "gender": 1,
    "id": 1367241,
    "name": "Amanda Clayton",
    "order": 25
}, {
    "cast_id": 170,
    "character": "Apache #1 (as Joe Billingiere)",
    "credit_id": "595ad40c9251410bfa04831e",
    "gender": 0,
    "id": 1844319,
    "name": "Joseph Billingiere",
    "order": 26
}
]

my_names = [temp_dict['name'] for temp_dict in my_list]