Cannot open a json file I am creating inside a function
Question:
I am trying to obtain a json file from this function, that is filled with items from the data variable. This variable data should then be dumped into the "hero_stats.json" file and I ought to open it for visualizing in the end. What is going wrong here? I can see all variables and names matching each others, am I missing some kind of call to open the file?
import pandas as pd, requests, json
# get updated info on game heroes
def get_hero_stats():
data = requests.get("https://api.opendota.com/api/heroStats").json()
pd.DataFrame(data).to_json("hero_stats.json")
with open('hero_stats.json', 'w') as outfile:
json.dump(data, outfile)
get_hero_stats()
I get no errors from this, just a message in the end with
Process finished with exit code 0
Answers:
import pandas as pd
import requests
import json
# get updated info on game heroes
def get_hero_stats():
data = requests.get("https://api.opendota.com/api/heroStats").json()
pd.DataFrame(data).to_json("hero_stats.json")
with open('hero_stats.json', 'r') as outfile:
return outfile
get_hero_stats()
The function is returning None
. You need to explicitly return a value, and assign it to something if you want to use it later.
import pandas as pd
import requests
# get updated info on game heroes
def get_hero_stats():
data = requests.get("https://api.opendota.com/api/heroStats").json()
pd.DataFrame(data).to_json("hero_stats.json")
return data
data = get_hero_stats()
# Do stuff with data
Also there is no need to write the data twice, so I removed the second write operation…
I am trying to obtain a json file from this function, that is filled with items from the data variable. This variable data should then be dumped into the "hero_stats.json" file and I ought to open it for visualizing in the end. What is going wrong here? I can see all variables and names matching each others, am I missing some kind of call to open the file?
import pandas as pd, requests, json
# get updated info on game heroes
def get_hero_stats():
data = requests.get("https://api.opendota.com/api/heroStats").json()
pd.DataFrame(data).to_json("hero_stats.json")
with open('hero_stats.json', 'w') as outfile:
json.dump(data, outfile)
get_hero_stats()
I get no errors from this, just a message in the end with
Process finished with exit code 0
import pandas as pd
import requests
import json
# get updated info on game heroes
def get_hero_stats():
data = requests.get("https://api.opendota.com/api/heroStats").json()
pd.DataFrame(data).to_json("hero_stats.json")
with open('hero_stats.json', 'r') as outfile:
return outfile
get_hero_stats()
The function is returning None
. You need to explicitly return a value, and assign it to something if you want to use it later.
import pandas as pd
import requests
# get updated info on game heroes
def get_hero_stats():
data = requests.get("https://api.opendota.com/api/heroStats").json()
pd.DataFrame(data).to_json("hero_stats.json")
return data
data = get_hero_stats()
# Do stuff with data
Also there is no need to write the data twice, so I removed the second write operation…