如何在Python中的字符串中打印内容?

我正在使用Python 3.8和Requests模块测试Rapidapi.com的API。这是代码:

import requests

url = "https://netflix-unofficial.p.rapidapi.com/api/genres"

headers = {
    'x-rapidapi-host': "netflix-unofficial.p.rapidapi.com",
    'x-rapidapi-key': //here goes my private key
}

response = requests.request("GET", url, headers=headers)

data = response.text

print(data)

我获得的输出是这样的:

{"data":["Action & Adventure","Anime Features","Children & Family Movies","Classic Movies","Cult Movies","Documentaries","Dramas","Faith & Spirituality","Horror Movies","Independent Movies","International Movies","LGBTQ Movies","Movies","Music & Musicals","Romantic Movies","Sci-Fi & Fantasy","Sports Movies","Stand-Up Comedy","Thrillers"]}

但是我希望能够访问列出的每个流派,并改为这样显示:

- Genre 1
- Genre 2
...
- Genre n 
评论
Yes
Yes

流派似乎作为数组中的单个元素存储在变量数据中。您可以为数据变量的长度运行一个循环,然后打印各个元素:

print(data[i])
点赞
评论
墨小沫
墨小沫

您的字符串在字典中。

Gene_Dict = {"data":["Action & Adventure","Anime Features","Children & Family Movies","Classic Movies","Cult Movies","Documentaries","Dramas","Faith & Spirituality","Horror Movies","Independent Movies","International Movies","LGBTQ Movies","Movies","Music & Musicals","Romantic Movies","Sci-Fi & Fantasy","Sports Movies","Stand-Up Comedy","Thrillers"]}
for ele in Gene_Dict[Gene_Dict.keys()[0]]: print ele
点赞
评论