【问题标题】:Extracting conditional values from json file从json文件中提取条件值
【发布时间】:2015-12-05 11:28:23
【问题描述】:

我创建了以下代码来解析 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 项奥斯卡奖的喜剧电影中扮演的演员的姓名。通过查看我的代码,它看起来对我来说是正确的,希望有人能解释一下。

【问题讨论】:

    标签: python json file


    【解决方案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')
    

    【讨论】:

    • 谢谢你,Amar 我现在看到了我的错误,至少我知道我离得不远了。
    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 2021-12-10
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多