I am wondering how I could print a certain value of a variable on a text string. Here is my code.
import requests
import time
import json
urls = ["https://api.opensea.io/api/v1/collection/doodles-official/stats",
"https://api.opensea.io/api/v1/collection/boredapeyachtclub/stats",
"https://api.opensea.io/api/v1/collection/mutant-ape-yacht-club/stats",
"https://api.opensea.io/api/v1/collection/bored-ape-kennel-club/stats"]
for url in urls:
response = requests.get(url)
json_data= json.loads(response.text)
data= (json_data["stats"]["floor_price"])
print("this is the floor price of {} of doodles!".format(data))
print("this is the floor price of {} of Bored Apes!".format(data))
As you can see in the code I am using the opensea API to extract the value of floor price from a json file of the different NFT collections listed on the urls. The variable if you print the variable data you will get the different prices as follow: The question is how can I take a specific value of that output, lets say 7.44 for example, and print in the following text
print("this is the floor price of {} of doodles!".format(data))
and then use the second value, 85, and print another text such as:
print("this is the floor price of {} of Bored Apes!".format(data))
How can I generate this output? Thanks everyone!
I tried using data[0] or data[1], but that doesn't work.
CodePudding user response:
The easiest way to do this in my opinion is to make another list, and add all the data to that list. For example, you can say data_list = []
to define a new list.
Then, in your for loop, add data_list.append(data)
, which adds the data to your list. Now, you can use data_list[0]
and data_list[1]
to access the different data.
The full code would look something like this:
import requests
import time
import json
urls = ["https://api.opensea.io/api/v1/collection/doodles-official/stats",
"https://api.opensea.io/api/v1/collection/boredapeyachtclub/stats",
"https://api.opensea.io/api/v1/collection/mutant-ape-yacht-club/stats",
"https://api.opensea.io/api/v1/collection/bored-ape-kennel-club/stats"]
data_list = []
for url in urls:
response = requests.get(url)
json_data= json.loads(response.text)
data= (json_data["stats"]["floor_price"])
data_list.append(data)
print("this is the floor price of {} of doodles!".format(data_list[0]))
print("this is the floor price of {} of Bored Apes!".format(data_list[1]))
The reason you cannot use data[0]
and data[1]
is because the variable data is rewritten each time the for loop runs again, and nothing is added.