csv Python合并多个自定义对象以导出到单个文件

mmvthczy  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(105)

我正在尝试将多个XBRL文件合并到一个Excel/CSV导出中。要合并的文件数量是可变的,取决于指定文件夹中的文件数量。该代码用于将单个文件导出到csv。唯一的区别是文件日期和值,文件中的所有代码/密钥都是相同的。我被困在如何循环通过file_list并将数据合并/解析为导出格式上。第二个目标是创建一个包含所有单个报告dict的dict。

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\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')
ss2ws0br

ss2ws0br1#

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')

相关问题