How to use external data stock in SoloLearn? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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!

9th Sep 2020, 4:35 PM
Coder+
6 Answers
+ 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.
9th Sep 2020, 5:42 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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).
9th Sep 2020, 5:59 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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())
9th Sep 2020, 5:19 PM
Steven M
Steven M - avatar
+ 1
Thank you! It worked.
9th Sep 2020, 6:06 PM
Coder+
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
9th Sep 2020, 5:52 PM
Coder+
0
Coder+ , Welcome :) If you are still confused how it works avoid copy pasting my solution. It's not a good idea to add some code in your program that you don't understand ;) Learn how urllib.request works here : https://docs.python.org/3.1/howto/urllib2.html
9th Sep 2020, 6:10 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar