2015-12-05 78 views
1

我已經創建了以下代碼來解析JSON文件並提取滿足特定條件的某些值並將它們放入文本文件中。我的代碼運行沒有錯誤,對我來說看起來沒問題。但是,當我打開文本文件時,它是空白的。從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 OSCARS發揮演員的名字。從我的代碼看,它看起來很適合我,希望有人能解釋。

回答

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') 
+0

謝謝阿馬爾我現在看到我的錯誤,至少我知道我不是太遙遠。 – colin