S9a8m added a comment.

So the last one still had horrible bugs, this one is better(-ish) I promise:

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 27 11:42:39 2018

@author: Sam
"""
import requests
import pandas as pd
import json

nomass_pubchems = [int(chem["pubchem"]) for chem in json.loads(open("C:/Users/Sam/Desktop/no_mass_chems (1).json").read())]

chemicals=[]
for n in range(201,300):
    print("Processing PubChem compound #{:}".format(nomass_pubchems[n]))
    try:
        file = requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/"+str(nomass_pubchems[n])+"/JSON/?")
        data = ""
        name = ("".join(data.split('"Name": "Record Title",')[1:])).split('"StringValue": "')[1].split('"')[0]
        #print(name)
        try:
            IUPACname = ("".join(data.split('"Name": "IUPAC')[1:])).split('Value": "')[1].split('"')[0]
            #print(IUPACname)
        except:
            IUPACname = 'N/A'
        formula = ("".join(data.split('"Name": "Molecular Formula"')[1:])).split('"StringValue": "')[1].split('"')[0]
        #print(formula)
        molWeight = data.split('"Molecular Weight')[1].split('NumValue": ')[1].split(',')[0]
        #print(molWeight)
        def_stereocount = ("".join(data.split('"Defined Atom Stereocenter Count"')[1:])).split('NumValue": ')[1].split('\n')[0]
        #print(def_stereocount)
        
        chemicals.append([name, IUPACname, formula, molWeight, def_stereocount])
    except Exception as e:
        print(e)
        print("Exception raised: PubChem compound #{:}".format(nomass_pubchems[n]))
    
col_name=["name", "IUPAC", "Formulae", "Mr", "Stereo"]
df = pd.DataFrame(chemicals, columns=col_name)

df.to_csv('C:/Users/Sam/Desktop/chemical_database2.csv', sep=',', index=False)
print(df)

TASK DETAIL
https://phabricator.wikimedia.org/T208036

EMAIL PREFERENCES
https://phabricator.wikimedia.org/settings/panel/emailpreferences/

To: S9a8m
Cc: BorDeh, Vemonet, Freddytuxworth, Husky, Laffano, S9a8m, A_ka_es, Teffubud, Dinadineke, Arybolab, Dja, Elvalente, Nandana, tabish.shaikh91, Lahi, Gq86, GoranSMilovanovic, Soteriaspace, Jayprakash12345, JakeTheDeveloper, QZanden, merbst, LawExplorer, DDJJ, Harmonia_Amanda, Spinster, Jane023, Wikidata-bugs, aude, TheDJ, Mbch331, valhallasw
_______________________________________________
Wikidata-bugs mailing list
Wikidata-bugs@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikidata-bugs

Reply via email to