I'm making an API call and I'm getting an None
value as I loop through my file.
I would place my csv in here, but it's over 100k records.
My code:
import csv
import xml.etree.ElementTree as ET
import xml.dom.minidom
import requests
import json
import pandas as pd
from pandas.io.json import json_normalize
#Storing results
api_results = []
error_results = []
none_results = []
print('Starting XML loop')
with open("C:/Users/template_v2.csv") as f:
reader = csv.DictReader(f)
# WRITING TO XML NODES
for i, row in enumerate(reader, start=1):
# INITIALIZING XML FILE
root = ET.Element('cbcalc')
icdNode = ET.SubElement(root, "icdcodes")
#handling first level ICD codes
for code in row['D'].split('~'):
ET.SubElement(icdNode, "code").text = code
#handling Client ID, state, country, age, job class and output
ET.SubElement(root, "clientid").text = row['CLAIM_NUM']
ET.SubElement(root, "state").text = row['BEN_ST']
ET.SubElement(root, "country").text = "US"
ET.SubElement(root, "age").text = row['AGE']
ET.SubElement(root, "jobclass").text = "1"
ET.SubElement(root, "fulloutput").text ="Y"
#handling the cfactors:
cfNode = ET.SubElement(root, "cfactors")
for k in ['legalrep', 'depression', 'diabetes',
'hypertension', 'obesity', 'smoker', 'subabuse']:
ET.SubElement(cfNode, k.lower()).text = str(row[k])
psNode = ET.SubElement(root, "prosummary")
psicdNode = ET.SubElement(psNode, "icd")
for code in row['P'].split('~'):
ET.SubElement(psNode, "code").text = code
psndcNode = ET.SubElement(psNode, "ndc")
for code in row['NDC_codes'].split('~'):
ET.SubElement(psNode, "code").text = code
cptNode = ET.SubElement(psNode, "cpt")
for code in row['CPT_codes'].split('~'):
ET.SubElement(cptNode, "code").text = code
ET.SubElement(psNode, "hcpcs")
doc = ET.tostring(root, method='xml', encoding="UTF-8")
response = requests.post(target_url, data=doc, headers=login_details)
response_data = json.loads(response.text)
if type(response_data)==dict and 'error' in response_data.keys():
error_results.append(response_data)
elif response_data == None or response_data == '':
none_results.append(response_data)
else:
api_results.append(response_data)
print('creating dataframe')
strategic_df = pd.json_normalize(api_results)
print("Writing out csv file")
strategic_df.to_csv(r'C:\Users\_dataframe2.csv', index = False, header=True)
Here is my error message:
Traceback (most recent call last):
File "c:\Users\Python\sc_ras_api.py", line 66, in <module>
response_data = json.loads(response.text)
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What did I break and how do I fix it?