Python 如何循环遍历整个JSON文件并将数据提取到变量中

Python 如何循环遍历整个JSON文件并将数据提取到变量中,python,json,Python,Json,我正在处理一个python文件,该文件从JSON文件中提取电影及其详细信息,然后将数据保存到自定义电影对象。现在,我可以从巨大的列表中选择一部电影 然而,我希望能够循环并获得每一种类型、导演、演员,并将它们添加到一个单独的数组中。现在,当我尝试执行此操作时,会出现以下错误: Traceback (most recent call last): File "/Users/leoconnelly/PycharmProjects/MLFinal/tester.py", line 27, i

我正在处理一个python文件,该文件从JSON文件中提取电影及其详细信息,然后将数据保存到自定义电影对象。现在,我可以从巨大的列表中选择一部电影

然而,我希望能够循环并获得每一种类型、导演、演员,并将它们添加到一个单独的数组中。现在,当我尝试执行此操作时,会出现以下错误:

    Traceback (most recent call last):
  File "/Users/leoconnelly/PycharmProjects/MLFinal/tester.py", line 27, in <module>
    tempGenre = (contents['results'][i]['genre'])
TypeError: list indices must be integers or slices, not str
以下是我的json数据:

{
  "results": [
    {
      "title": "After Dark in Central Park",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Boarding School Girls' Pajama Parade",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Buffalo Bill's Wild West Parad",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Caught",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Clowns Spinning Hats",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Capture of Boer Battery by British",
      "year": 1900,
      "director": "James H. White",
      "cast": null,
      "genre": "Short documentary",
      "notes": null
    },
    {
      "title": "The Enchanted Drawing",
      "year": 1900,
      "director": "J. Stuart Blackton",
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Family Troubles",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "Feeding Sea Lions",
      "year": 1900,
      "director": null,
      "cast": "Paul Boyton",
      "genre": null,
      "notes": null
    },
    {
      "title": "How to Make a Fat Wife Out of Two Lean Ones",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": "Comedy",
      "notes": null
    },
    {
      "title": "New Life Rescue",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    },
    {
      "title": "New Morning Bath",
      "year": 1900,
      "director": null,
      "cast": null,
      "genre": null,
      "notes": null
    }
  ]
}

对于范围内的i(len(content['results')),您需要
,然后
content['results'][i]
将用作
列表索引必须是整数

当您在content
中为i执行
操作时,您正在内容字典的键上循环,这些键是字符串


但是,
contents['results']
是一个列表。您可以将它们作为完整对象循环,而不是获取特定的数字索引

这使用列表理解从结果列表中获取电影对象的完整列表

with open('movies.json') as f:
    contents = json.load(f)
    results = contents.get('results', [])
    movies = [ 
       Movie(
           r.get('title'),
           r.get('director'),
           r.get('genre'),
           r.get('cast')
       ) for r in results ]
    for m in movies:
        print(m.name)
我希望能够循环并获得每一个流派、导演、演员,并将它们添加到一个单独的数组中

您可以从制作的电影阵列中执行类似的操作

这将通过在列表中设置
对象来返回所有电影的唯一导演

directors = list(set(m.director for m in movies if m.director is not None))

目录
是一本字典。。。您对目录中的i的
期望是什么?
directors = list(set(m.director for m in movies if m.director is not None))