简体   繁体   中英

Extracting values from JSON file Python Error

I'm trying to extract certain values from a JSON file. Here is my code.

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)

I'm trying to extract the name of a director who directed a movie in 2014. However, when I run the above code I am given the following error.

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

Can anyone explain why I am getting this error from my code?

Here is the JSON file

{
"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
            }
        }
    }
}

}

Thank you.

Try just iterator as key to 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')

Output file content-

Frank Coraci
Emilio Lazaro
Woody Allen
Alfonso Cuaron

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM