从json文件中提取条件值

时间:2015-12-05 11:28:23

标签: python json file

我创建了以下代码来解析JSON文件并提取满足特定条件的某些值并将它们放入文本文件中。我的代码运行没有错误,对我来说看起来不错。但是,当我打开文本文件时,它是空白的。

def my_main(ifile_name, ofile_name):

ifile = open(ifile_name, 'r')
ofile = open(ofile_name, "w")
json_decode=json.load(ifile)
result = []
for i in json_decode['Culture']['Movies']:
    for k in json_decode['Culture']['Movies'][i]:
        if "Oscars" in json_decode['Culture']['Movies'][i][k] == 0 and "Genre" in json_decode['Culture']['Movies'][i][k] == Comedy:
            data = "Actors" in json_decode['Culture']['Movies'][i][k]
            print data
            result.append(data)

for j in result:
    ofile.write(j+'\n')

JSON文件如下

{
    "Culture": {
        "Movies": {
            "2015": {
                "Birdman": {
                    "Genre": "Comedy",
                    "Director": "Alejandro Inarritu",
                    "Oscars": 9,
                    "Actors": [
                        "Michael Keaton",
                        "Enma Stone",
                        "Edward Norton",
                        "Naomi Watts"
                    ]
                },
                "The Imitation Game": {
                    "Genre": "Drama",
                    "Director": "Morten Tyldum",
                    "Oscars": 8,
                    "Actors": [
                        "Benedict Cumberbatch",
                        "Keira Knightley",
                        "Matthew Goode"
                    ]
                },
                "Magic in the Moonlight": {
                    "Genre": "Comedy",
                    "Director": "Woody Allen",
                    "Oscars": 0,
                    "Actors": [
                        "Enma Stone",
                        "Colin Firth",
                        "Marcia Harden"
                    ]
                }
            },
            "2014": {
                "Gravity": {
                    "Genre": "Drama",
                    "Director": "Alfonso Cuaron",
                    "Oscars": 10,
                    "Actors": [
                        "Sandra Bullock",
                        "George Clooney",
                        "Ed Harris",
                        "Paul Sharma"
                    ]
                },
                "Blue Jasmine": {
                    "Genre": "Comedy",
                    "Director": "Woody Allen",
                    "Oscars": 1,
                    "Actors": [
                        "Cate Blanchett",
                        "Sally Hawkins",
                        "Alec Baldwin"
                    ]
                },
                "Blended": {
                    "Genre": "Romance",
                    "Director": "Frank Coraci",
                    "Oscars": 0,
                    "Actors": [
                        "Adam Sandler",
                        "Drew Barrymore",
                        "Jack Giarraputo"
                    ]
                },
                "Ocho Apellidos Vascos": {
                    "Genre": "Comedy",
                    "Director": "Emilio Lazaro",
                    "Oscars": 0,
                    "Actors": [
                        "Dani Rovira",
                        "Clara Lago",
                        "Karra Elejalde",
                        "Carmen Machi"
                    ]
                }
            }
        },
        "Books": {
            "2015": {
                "Go Set a Watchman": {
                    "Genre": "Fiction",
                    "Author": "Harper Lee",
                    "Pages": 278
                },
                "The Girl on the Train": {
                    "Genre": "Thriller",
                    "Author": "Paula Hawkins",
                    "Pages": 320
                }
            },
            "2014": {
                "El Barco de los Ninos": {
                    "Genre": "Children",
                    "Author": "Mario Llosa",
                    "Pages": 96
                },
                "Sapiens": {
                    "Genre": "History",
                    "Author": "Yuval Harari",
                    "Pages": 464
                }
            }
        }
    }
}

我正在试图获得在COMEDY电影中播放的ACTORS的名字,该电影赢得了0 OSCARS。从查看我的代码看起来对我来说,希望有人可以解释。

1 个答案:

答案 0 :(得分:1)

首先,你给的json文件最后有一个缺失的括号。其次,这是工作代码。你在if条件和数据初始化时错了。

import json
def my_main(ifile_name, ofile_name):
  ifile = open(ifile_name, 'r')
  ofile = open(ofile_name, "w")
  json_decode=json.load(ifile)
  result = []
  for i in json_decode['Culture']['Movies']:
      for k in json_decode['Culture']['Movies'][i]:
          if json_decode['Culture']['Movies'][i][k]['Oscars'] == 0  and  json_decode['Culture']['Movies'][i][k]['Genre'] == "Comedy":
            data = json_decode['Culture']['Movies'][i][k]['Actors']
            print data
            result.append(data)

for j in result:
    ofile.write(str(j)+str('\n'))

my_main('movies.json','o.txt')