2015-12-04 61 views
0

我试图从JSON文件中提取某些值。这是我的代码。从JSON文件中提取值Python错误

ifile = open(ifile_name, 'r') 

json_decode=json.load(ifile) 
result = [] 
for item in json_decode: 
    my_dict={} 
    my_dict['Culture']['Movies']['2014']['Gravity']= item.get('Director') 
    my_dict['Culture']['Movies']['2014']['Blue Jasmine'] = item.get('Director') 
    print my_dict 
    result.append(my_dict) 
    back_jason=json.dumps(result, ofile) 
    with open(ofile_name, "w+") as file : 
     file.write(back_jason) 

我试图提取导演谁在2014年执导的影片不过的名字,当我运行上面的代码,我给下面的错误。

my_dict['Culture']['Movies']['2014']['Gravity']= item.get('Director') 
AttributeError: 'unicode' object has no attribute 'get' 

任何人都可以解释为什么我从我的代码中得到这个错误?

这里是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

帖子什么是文件即样本文件内容 – SIslam

+0

@SIslam我有内容更新的问题的JSON文件,抱歉。 – colin

+0

你能澄清一下你想写在第二个文件(ofile_name)中吗? - 只是导演的名字?您正试图以不好的方式构建字典(my_dict)。 – SIslam

回答

3

尽量只迭代器作为关键dictionary-

import json 

ifile = open(r"D:\tst.txt", 'r') 

json_decode=json.load(ifile) 
result = [] 
for i in json_decode['Culture']['Movies']['2014']: 
    data = json_decode['Culture']['Movies']['2014'][i]['Director'] 
    print data 
    result.append(data) 

with open(r"D:\tst1.txt", "w+") as file : 
    for j in result: 
     file.write(j+'\n') 

输出文件的内容 -

Frank Coraci 
Emilio Lazaro 
Woody Allen 
Alfonso Cuaron 
+0

这很好,现在我有了更好的理解。谢谢! – colin