如何在Python中的字符串内打印内容?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在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
答案
似乎流派作为数组中的单个元素存储在变量数据中。您可以为数据变量的长度运行一个循环,然后打印各个元素:
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
以上是关于如何在Python中的字符串内打印内容?的主要内容,如果未能解决你的问题,请参考以下文章
片段中 ListView 的 setOnItemClickListener
python python中的漂亮(或漂亮打印)JSON对象具有这一功能。在尝试了解什么时,我总是使用这个片段
我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情