Python merge multiple custom objects for export to single file

63 Views Asked by At

I am attempting to merge multiple XBRL files into a single excel/csv export. The number of files to be merged is variable and will be dependent on how many are in the specified folder. The code works to export a single file to csv. The only differentiations are the file date and values, all codes/keys in the file are the same. I am stuck on how to cycle through the file_list and consolidate/parse the data into a format for export. A secondary goal is to create a dict containing all the individual report dicts.

import os, glob, pandas as pd, xbrl, csv
from xbrl import XBRLParser, GAAP, GAAPSerializer

path = 'P:/Bank Research/Bank Automation/Calls'

callCert = '57944'
dates = ['063023','123122','123121','123120','123119','123118']
xbrl_parser = XBRLParser()


file_list = glob.glob(path + "/*.XBRL")

['P:/Bank Research/Bank Automation/Calls\Call_Cert57944_033123.XBRL', Call_Cert57944_063023.XBRL', Call_Cert57944_123118.XBRL', Call_Cert57944_123119.XBRL',

xbrl_file = "Call_Cert"+callCert+"_"+dates[0]+".XBRL"    
xbrl_document = xbrl_parser.parse(xbrl_file)
custom_obj = xbrl_parser.parseCustom(xbrl_document)  


list_bank_numbers = []
list_bank_keys = []
for i in custom_obj():
    list_bank_numbers.append(i[1])
    list_bank_keys.append(i[0])

bank_dict = {list_bank_keys[i]: list_bank_numbers[i] for i in range(len(list_bank_keys))}


def export_dict_to_csv(dictionary, output_file):

    keys = dictionary.keys()
    values = dictionary.values()

    with open(output_file, 'w', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(keys)
        writer.writerow(values)
        
        
export_dict_to_csv(bank_dict, callCert+'.csv')
1

There are 1 best solutions below

2
yash On
import os, glob, pandas as pd, xbrl, csv
from xbrl import XBRLParser, GAAP, GAAPSerializer


# Create file list using either glob or manually
path = 'P:/Bank Research/Bank Automation/Calls'
file_list = glob.glob(path + "/*.XBRL")


callCert = '57944'
dates = ['063023','123122','123121','123120','123119','123118']
file_list = [f"{path}/Call_Cert{callCert}_{date_item}.XBRL" for date_item in dates]


# Add keys and numbers directly to the dictionary
xbrl_parser = XBRLParser()
bank_dict = {}

for xbrl_file in file_list:
    xbrl_document = xbrl_parser.parse(xbrl_file)
    custom_obj = xbrl_parser.parseCustom(xbrl_document)

    for i in custom_obj():
        bank_dict[i[0]] = i[1]

# Note that having the same keys will overwrite the previous value
# to handle such cases you can use the following ideas:
# before adding the key check if it exists in the dictionary
bank_dict.get(i[0], None)
# this returns None when the key doesn't exist
# so using conditions such as 
if bank_dict.get(i[0], None) is None:
    bank_dict[i[0]] = i[1]
else:
    # do something else
    pass


export_dict_to_csv(bank_dict, callCert+'.csv')