简体   繁体   English

在 python3 中遍历 json 文件

[英]iterating through a json file in python3

I am learning how to parse through different files (xml, json, csv) and grabbing specific values in Python.我正在学习如何解析不同的文件(xml、json、csv)并获取 Python 中的特定值。 However, I do not know how to go about writing code to find things such as which movie summary has the most characters or the total number of movies.但是,我不知道如何编写代码来查找诸如哪个电影摘要中字符最多或电影总数之类的东西。 I was wondering if somebody could please point me in the right direction or if there is a video that helps with grabbing info from a json file.我想知道是否有人可以为我指出正确的方向,或者是否有一个视频可以帮助从 json 文件中获取信息。

Here's the json file:这是 json 文件:

[
    {
        "title": "A History of Violence",
        "year": "2005",
        "genre": "Crime",
        "summary": "Tom Stall, a humble family man and owner of a popular neighborhood restaurant, lives a quiet but fulfilling existence in the Midwest. One night Tom foils a crime at his place of business and, to his chagrin, is plastered all over the news for his heroics. Following this, mysterious people follow the Stalls' every move, concerning Tom more than anyone else. As this situation is confronted, more lurks out over where all these occurrences have stemmed from compromising his marriage, family relationship and the main characters' former relations in the process.",
        "country": "USA",

        "director": {
            "last_name": "Cronenberg",
            "first_name": "David",
            "birth_date": "1943"
        },
        "actors": [
            {
                "first_name": "Ed",
                "last_name": "Harris",
                "birth_date": "1950",
                "role": "Carl Fogarty"
            },
            {
                "first_name": "Vigo",
                "last_name": "Mortensen",
                "birth_date": "1958",
                "role": "Tom Stall"
            },
            {
                "first_name": "Maria",
                "last_name": "Bello",
                "birth_date": "1967",
                "role": "Eddie Stall"
            },
            {
                "first_name": "William",
                "last_name": "Hurt",
                "birth_date": "1950",
                "role": "Richie Cusack"
            }
        ]
    },
    {
        "title": "Marie Antoinette",
        "year": "2006",
        "genre": "Drama",
        "summary": "Based on Antonia Fraser's book about the ill-fated Archduchess of Austria and later Queen of France, 'Marie Antoinette' tells the story of the most misunderstood and abused woman in history, from her birth in Imperial Austria to her later life in France.",
        "country": "USA",

        "director": {
            "last_name": "Coppola",
            "first_name": "Sofia",
            "birth_date": "1971"
        },
        "actors": [
            {
                "first_name": "Kirsten",
                "last_name": "Dunst",
                "birth_date": "1982",
                "role": "Marie Antoinette"
            },
            {
                "first_name": "Jason",
                "last_name": "Schwartzman",
                "birth_date": "1980",
                "role": "Louis XVI"
            }
        ]
    },
    {
        "title": "Spider-Man",
        "year": "2002",
        "genre": "Action",
        "summary": "On a school field trip, Peter Parker (Maguire) is bitten by a genetically modified spider. He wakes up the next morning with incredible powers. After witnessing the death of his uncle (Robertson), Parkers decides to put his new skills to use in order to rid the city of evil, but someone else has other plans. The Green Goblin (Dafoe) sees Spider-Man as a threat and must dispose of him.",
        "country": "USA",

        "director": {
            "last_name": "Raimi",
            "first_name": "Sam",
            "birth_date": "1959"
        },
        "actors": [
            {
                "first_name": "Tobey",
                "last_name": "Maguire",
                "birth_date": "1975",
                "role": "Peter Parker"
            },
            {
                "first_name": "Tobey",
                "last_name": "Maguire",
                "birth_date": "1975",
                "role": "Spider-Man"
            },
            {
                "first_name": "Kirsten",
                "last_name": "Dunst",
                "birth_date": "1982",
                "role": "Mary Jane Watson"
            },
            {
                "first_name": "Willem",
                "last_name": "Dafoe",
                "birth_date": "1955",
                "role": "Green Goblin"
            },
            {
                "first_name": "Willem",
                "last_name": "Dafoe",
                "birth_date": "1955",
                "role": "Norman Osborn"
            }
        ]
    },
    {
        "title": "The Social network",
        "year": "2010",
        "genre": "Drama",
        "summary": "On a fall night in 2003, Harvard undergrad and computer programming genius Mark Zuckerberg sits down at his computer and heatedly begins working on a new idea. In a fury of blogging and programming, what begins in his dorm room soon becomes a global social network and a revolution in communication. A mere six years and 500 million friends later, Mark Zuckerberg is the youngest billionaire in history... but for this entrepreneur, success leads to both personal and legal complications.",
        "country": "USA",
        "director": {
            "last_name": "Fincher",
            "first_name": "David",
            "birth_date": "1962"
        },
        "actors": [
            {
                "first_name": "Jesse",
                "last_name": "Eisenberg",
                "birth_date": "1983",
                "role": "Mark Zuckerberg"
            },
            {
                "first_name": "Rooney",
                "last_name": "Mara",
                "birth_date": "1985",
                "role": "Erica Albright"
            },
            {
                "first_name": "Andrew",
                "last_name": "Garfield",
                "birth_date": "1983",
                "role": "Eduardo Saverin"
            },
            {
                "first_name": "Justin",
                "last_name": "Timberlake",
                "birth_date": "1981",
                "role": "Sean Parker"
            }
        ]
    },
    {
        "title": "Unforgiven",
        "year": "1992",
        "genre": "Western",
        "summary": "The town of Big Whisky is full of normal people trying to lead quiet lives. Cowboys try to make a living. Sheriff 'Little Bill' tries to build a house and keep a heavy-handed order. The town whores just try to get by.Then a couple of cowboys cut up a whore. Unsatisfied with Bill's justice, the prostitutes put a bounty on the cowboys. The bounty attracts a young gun billing himself as 'The Schofield Kid', and aging killer William Munny. Munny reformed for his young wife, and has been raising crops and two children in peace. But his wife is gone. Farm life is hard. And Munny is no good at it. So he calls his old partner Ned, saddles his ornery nag, and rides off to kill one more time, blurring the lines between heroism and villainy, man and myth.",
        "country": "USA",

        "director": {
            "last_name": "Eastwood",
            "first_name": "Clint",
            "birth_date": "1930"
        },
        "actors": [
            {
                "first_name": "Clint",
                "last_name": "Eastwood",
                "birth_date": "1930",
                "role": "William Munny"
            },
            {
                "first_name": "Gene",
                "last_name": "Hackman",
                "birth_date": "1930",
                "role": "Little Bill Dagget"
            },
            {
                "first_name": "Morgan",
                "last_name": "Freeman",
                "birth_date": "1937",
                "role": "Ned Logan"
            }
        ]
    }
]

There's a package called json .有一个名为 json 的json You can use that.你可以用那个。

This is a good article on working with json data in python这是一篇关于在 python 中使用 json 数据的好文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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