python - 从 json 文件中提取条件值

标签 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
                }
            }
        }
    }
}

我正在尝试获取在一部获得 0 项奥斯卡奖的喜剧电影中出演的 Actor 的姓名。从我的代码来看,它看起来对我来说是正确的,希望有人能解释一下。

最佳答案

首先,您提供的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')

关于python - 从 json 文件中提取条件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34104325/

相关文章:

python - 使用 Python 的 Apple 推送通知服务

python - 如何默认在 GPU 上运行 PyTorch?

python - 无序集合 - 在 python 中设置

python - 如何在python中引用类变量

IOS app Performance is too slow for parse data with swiftyjson

java - 使用扫描仪和循环读取文件时出现问题

c - 在 C 中确定文件类型

ios - 如何接收 json 字符串并将其转换为字典?

javascript - 获取 JSON 中数组对象的名称

ruby - 需要 ruby​​ 目录中所有文件的最佳方法?