0

I'm trying to iterate through an excel sheet and find the NDC of a drug from its generic name in each cell in a column so that I don't have to manually type in each generic name. This is what I have so far, but the parameter value is not recognized as a valid key. Is there a syntax problem or something else wrong?

wrkbk = openpyxl.load_workbook('data.xlsx')
sh = wrkbk.active

for i in range(1, sh.max_row):

    cell_obj = sh.cell(row=i+1, column=3)
    parameter = cell_obj.value

    response = requests.get('https://api.fda.gov/drug/ndc.json?search=generic_name:',parameter)    

    jprint(response.json())

1 Answer 1

1

I was able to confirm that the API is working using the name 'ibuprofen':

curl -g "https://api.fda.gov/drug/ndc.json?search=generic_name:'ibuprofen'"

This gave a json response about ibuprofen, as expected.

I suspect that the problem is in your Python code, in the way in which you are passing the parameter to the requests.get function. Some troubleshooting in an ipython session confirms this (see below).

One quick fix that I found is to use python string formatting (also see below).

In [1]: import requests

In [2]: requests.get? 
Signature: requests.get(url, params=None,**kwargs) 
Docstring: 
Sends a GET request.

:param url: URL for the new :class:`Request` object. 
:param params: (optional) Dictionary, list of tuples or bytes to send
    in the query string for the :class:`Request`. 
:param \*\*kwargs: Optional arguments that ``request`` takes. 
:return: :class:`Response <Response>` object 
:rtype: requests.Response 
File:      /opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/api.py 
Type:      function

In [3]: parameter="ibuprofen"

In [4]: response = requests.get('https://api.fda.gov/drug/ndc.json?search=generic_name:',parameter)

In [5]: print(response.json()) 
{'error': {'code': 'BAD_REQUEST', 'message': 'Invalid parameter: ibuprofen'}}

In [6]: response = requests.get(f'https://api.fda.gov/drug/ndc.json?search=generic_name:"{parameter}"')

In [7]: print(response.json()) 
{'meta': {'disclaimer': 'Do not rely on openFDA to make decisions regarding medical care. While we make every effort to ensure that data is accurate, you should assume all results are unvalidated. We may limit or otherwise restrict your access to the API in line with our Terms of Service.', 'terms': 'https://open.fda.gov/terms/', 'license': 'https://open.fda.gov/license/', 'last_updated': '2022-06-14', 'results': {'skip': 0, 'limit': 1, 'total': 1443}}, 'results': [{'product_ndc': '72189-266', 'generic_name': 'IBUPROFEN', 'labeler_name': 'DIRECT RX', 'brand_name': 'IBUPROFEN', 'active_ingredients': [{'name': 'IBUPROFEN', 'strength': '800 mg/1'}], 'finished': True, 'packaging': [{'package_ndc': '72189-266-15', 'description': '15 TABLET, FILM COATED in 1 BOTTLE (72189-266-15)', 'marketing_start_date': '20210916', 'sample': False}, {'package_ndc': '72189-266-30', 'description': '30 TABLET, FILM COATED in 1 BOTTLE (72189-266-30)', 'marketing_start_date': '20210916', 'sample': False}, {'package_ndc': '72189-266-40', 'description': '40 TABLET, FILM COATED in 1 BOTTLE (72189-266-40)', 'marketing_start_date': '20210916', 'sample': False}, {'package_ndc': '72189-266-60', 'description': '60 TABLET, FILM COATED in 1 BOTTLE (72189-266-60)', 'marketing_start_date': '20210916', 'sample': False}, {'package_ndc': '72189-266-71', 'description': '100 TABLET, FILM COATED in 1 BOTTLE (72189-266-71)', 'marketing_start_date': '20210916', 'sample': False}, {'package_ndc': '72189-266-72', 'description': '120 TABLET, FILM COATED in 1 BOTTLE (72189-266-72)', 'marketing_start_date': '20210916', 'sample': False}, {'package_ndc': '72189-266-90', 'description': '90 TABLET, FILM COATED in 1 BOTTLE (72189-266-90)', 'marketing_start_date': '20210916', 'sample': False}], 'listing_expiration_date': '20231231', 'openfda': {'manufacturer_name': ['DIRECT RX'], 'rxcui': ['197806', '197807'], 'spl_set_id': ['c3f30813-0848-6e8a-e053-2995a90a3dea'], 'nui': ['N0000000160', 'M0001335', 'N0000175722'], 'pharm_class_moa': ['Cyclooxygenase Inhibitors [MoA]'], 'pharm_class_cs': ['Anti-Inflammatory Agents, Non-Steroidal [CS]'], 'pharm_class_epc': ['Nonsteroidal Anti-inflammatory Drug [EPC]'], 'unii': ['WK2XYI10QM']}, 'marketing_category': 'ANDA', 'dosage_form': 'TABLET, FILM COATED', 'spl_id': 'd6a6d801-5a63-2aa7-e053-2995a90aebe9', 'product_type': 'HUMAN PRESCRIPTION DRUG', 'route': ['ORAL'], 'marketing_start_date': '20210916', 'product_id': '72189-266_d6a6d801-5a63-2aa7-e053-2995a90aebe9', 'application_number': 'ANDA090796', 'brand_name_base': 'IBUPROFEN', 'pharm_class': ['Anti-Inflammatory Agents', 'Non-Steroidal [CS]', 'Cyclooxygenase Inhibitors [MoA]', 'Nonsteroidal Anti-inflammatory Drug [EPC]']}]}

In [8]:

I hope this helps! ZK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.