'Why does .get() not download/save my reports as planned?
I'm trying to run an API call and download reports after matching the report name with the specific ID. Everything works fine when I don't use .get() to assign the IDs into final_id_list. But this way, the script crashes when the report does not exist. So I need to use .get() in order to input None when the report value is empty.
This works but does not catch the KeyError:
final_id_list = [id_title_dict['Daily Finance Report'],
id_title_dict['Weekly Finance Report'],
id_title_dict['Monthly Finance Report'],
id_title_dict['Yearly Finance Report']
]
This does not save my reports and throws no errors:
final_id_list = [id_title_dict.get('Daily Finance Report'),
id_title_dict.get('Weekly Finance Report'),
id_title_dict.get('Monthly Finance Report'),
id_title_dict.get('Yearly Finance Report'),
]
Printing both provides the exact same list. I am completely lost as to why the list created with .get() does not work as expected. It must be something in the rest of my code that I cannot see.
Please advise.
Full code:
import requests
import base64
import os
import xml.etree.ElementTree as ET
def api_call(payload):
headers = {
'X-Requested-With': "Python 3.8.0",
'Authorization': "Basic Authentication"
}
req = requests.post("https://API_URL_HERE",
params=payload,
headers=headers,
auth=("username", base64.b64decode(password)))
return req.text
payload_get_ID = {"action": "list"}
api_call(payload_get_ID)
root = ET.fromstring(api_call(payload_get_ID))
id_list = []
for id in root.iter('ID'):
id_list.append(id.text)
title_list = []
for rep_title in root.iter('TITLE'):
title_list.append(rep_title.text)
# dictionary matching all IDs to all report names
id_title_dict = dict(zip(title_list, id_list))
# list of relevant IDs for specified report names
# use .get() to input 'none' where report does not exist, but does not work
final_id_list = [id_title_dict.get('Daily Finance Report'),
id_title_dict.get('Weekly Finance Report'),
id_title_dict.get('Monthly Finance Report'),
id_title_dict.get('Yearly Finance Report'),
]
# payload to get reports via ID
payload_get_report0 = {"action": "fetch", "id": final_id_list[0]}
payload_get_report1 = {"action": "fetch", "id": final_id_list[1]}
payload_get_report2 = {"action": "fetch", "id": final_id_list[2]}
payload_get_report3 = {"action": "fetch", "id": final_id_list[3]}
# assigning the API response to variable
data0 = api_call(payload_get_report0)
data1 = api_call(payload_get_report1)
data2 = api_call(payload_get_report2)
data3 = api_call(payload_get_report3)
def save_as_csv_in_dir(report_name, data_num, dir_path):
os.makedirs(dir_path, exist_ok=True)
with open(os.path.join(dir_path, report_name + '.csv'), 'w', newline='', encoding="utf-8") as f:
f.write(data_num)
save_as_csv_in_dir('Daily Finance Report', data0,
'C:\\path\\to\\Desktop\\directory')
save_as_csv_in_dir('Weekly Finance Report', data1,
'C:\\path\\to\\Desktop\\directory')
save_as_csv_in_dir('Monthly Finance Report', data2,
'C:\\path\\to\\Desktop\\directory')
save_as_csv_in_dir('Yearly Finance Report', data3,
'C:\\path\\to\\Desktop\\directory')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
