python 如何访问此查询中的嵌套列表?

wztqucjr  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(110)

使用Flask的以下路径从MongoDB中提取:

@app.route('/search-all/', methods=['GET', 'POST']) # EDIT
def search_all():
    for x in db['mongo-moviedb'].find({"Films.Title" : "Clue"}, {"Films.$": 1}):
        result = x
        return render_template('Date-Night.html', result=result)

返回以下内容:

{'Films': [{'Actors': 'Eileen Brennan, Tim Curry, Madeline Kahn',
            'Awards': 'N/A',
            'BoxOffice': '$14,643,997',
            'Country': 'United States',
            'DVD': '27 Jun 2000',
            'Director': 'Jonathan Lynn',
            'Genre': 'Comedy, Crime, Mystery',
            'Language': 'English, French',
            'Metascore': '39',
            'Plot': 'Six guests are anonymously invited to a strange mansion '
                    'for dinner, but after their host is killed, they must '
                    'cooperate with the staff to identify the murderer as the '
                    'bodies pile up.',
            'Poster': 'https://m.media-amazon.com/images/M/MV5BM2VlNTE1ZmMtOTAyNS00ODYwLWFmY2MtZWEzOTE2YjE1NDE2XkEyXkFqcGdeQXVyNDk3NzU2MTQ@._V1_SX300.jpg',
            'Production': 'N/A',
            'Rated': 'PG',
            'Ratings': [{'Source': 'Internet Movie Database',
                         'Value': '7.2/10'},
                        {'Source': 'Rotten Tomatoes', 'Value': '68%'},
                        {'Source': 'Metacritic', 'Value': '39/100'}],
            'Released': '13 Dec 1985',
            'Response': 'True',
            'Runtime': '94 min',
            'Title': 'Clue',
            'Type': 'movie',
            'Website': 'N/A',
            'Writer': 'John Landis, Jonathan Lynn, Anthony E. Pratt',
            'Year': '1985',
            'imdbID': 'tt0088930',
            'imdbRating': '7.2',
            'imdbVotes': '96,112'}],
 '_id': ObjectId('638135b01687bf075645b0da')}

我不明白的是如何访问嵌套的Films列表,以获取特定的项(TitleYear等)并将其加载到单独的变量(xyz)中。
我试过x['Films']x['Films.Title']x['Films'],['Title'],但我不确定我在这里要找什么。谢谢,伙计们。

l0oc07j2

l0oc07j21#

x["Films"]是一个只有一个值的列表,因此:

x["Films"][0]["Title"]

相关问题