+ 2
How to use external data stock in SoloLearn?
I want to use the dictionary from this git-hub repository: https://raw.githubusercontent.com/Bowserinator/Periodic-Table-JSON/master/periodic-table-lookup.json in python. Thank you for answering!
6 Antworten
+ 3
I'm not a python programmer but after reading some C-Python documentation , Here is what I made. You don't need to install any libraries. urllib is sufficient to make an http request.
```
from urllib import request
import json
url = "https://raw.githubusercontent.com/Bowserinator/Periodic-Table-JSON/master/periodic-table-lookup.json"
res = request.urlopen(url)
data = json.loads(res.read())
print(data["hydrogen"])
```
I'll suggest you to add exception handling code because some methods used here , I guess ,can throw exceptions.
+ 2
Coder+ , The file is stored on internet somewhere else, not on the machine that is executing your code.
You can't use open() for that. As shown in my previous answer you can use request.urlopen(url).
+ 1
'''
It times out in the Playground. There might be another way, but you only have a few seconds before Sololearn forces a timeout.
'''
def install(m):
import os
return os.system(f'pip -q --disable-pip-version-check install {m}')
install('requests')
import pandas as pd
import requests
import json
url = "https://raw.githubusercontent.com/Bowserinator/Periodic-Table-JSON/master/periodic-table-lookup.json"
r=requests.get(url).text
data=json.loads(r)
print(data)
'''
another way that should work, but it doesnt, there is something wrong with the way the json is formed...
'''
import pandas as pd
url = "https://raw.githubusercontent.com/Bowserinator/Periodic-Table-JSON/master/periodic-table-lookup.json"
df=pd.read_json(url)
print(df.head())
+ 1
Thank you! It worked.
0
First: thank you for answering!!! I tried the following code but it returned "file not found"... I hoped there were an easier solution!
https://code.sololearn.com/cczyt68o6geW/?ref=app