gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:44:20 27 4
gpt4 key购买 nike

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

但是,我希望能够循环并获取每个流派、导演、 Actor 并将它们添加到单独的数组中。现在,当我尝试执行此操作时,我收到此错误:

    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

我还想创建一个包含标题、 Actor 、导演和流派的自定义电影对象数组。

这是我的代码:

from movie import Movie
from user import User
import json
from pprint import pprint


movieArray = []
nameArray = []
directorArray = []
genreArray = []
##actorArray = []

movieToBeInputted = Movie("","","","")


with open('movies.json') as f:
contents = json.load(f)
print(contents['results'][600]['title'])
movieToBeInputted.name = (contents['results'][600]['title'])
movieToBeInputted.director = (contents['results'][600]['director'])
movieToBeInputted.genre = (contents['results'][600]['genre'])
movieToBeInputted.actors = (contents['results'][600]['cast'])
movieArray.append(movieToBeInputted)


for i in contents:
tempGenre = (contents['results'][i]['genre'])
genreArray.append(tempGenre) #this is where the error happens

print("xxxxxxx")
print(movieToBeInputted.actors)




##d = json.load(json_data)

##json_movie_data = json.dumps(json_data)




##movieToBeInputted.actors = json_movie_data

这是我的 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
}
]
}

最佳答案

您需要for i in range(len(content['results'])) ,然后content['results'][i]将作为 list indices <b>must be integers</b>

当你这么做的时候for i in content ,您正在循环内容字典的键,这些键是字符串。

<小时/>

但是,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)

I want to be able to loop through and get every single genre, director, actor and add them to a separate array

您可以从您制作的电影数组中执行类似的操作。

这将通过创建 set 返回所有电影的唯一导演。对象放入列表中。

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

关于python - 如何循环遍历整个 JSON 文件并将数据提取到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com