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

mmvthczy  于 2023-10-13  发布在  Python
关注(0)|答案(1)|浏览(142)

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

  1. import os, glob, pandas as pd, xbrl, csv
  2. from xbrl import XBRLParser, GAAP, GAAPSerializer
  3. path = 'P:/Bank Research/Bank Automation/Calls'
  4. callCert = '57944'
  5. dates = ['063023','123122','123121','123120','123119','123118']
  6. xbrl_parser = XBRLParser()
  7. 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“,

  1. xbrl_file = "Call_Cert"+callCert+"_"+dates[0]+".XBRL"
  2. xbrl_document = xbrl_parser.parse(xbrl_file)
  3. custom_obj = xbrl_parser.parseCustom(xbrl_document)
  4. list_bank_numbers = []
  5. list_bank_keys = []
  6. for i in custom_obj():
  7. list_bank_numbers.append(i[1])
  8. list_bank_keys.append(i[0])
  9. bank_dict = {list_bank_keys[i]: list_bank_numbers[i] for i in range(len(list_bank_keys))}
  10. def export_dict_to_csv(dictionary, output_file):
  11. keys = dictionary.keys()
  12. values = dictionary.values()
  13. with open(output_file, 'w', newline='') as csv_file:
  14. writer = csv.writer(csv_file)
  15. writer.writerow(keys)
  16. writer.writerow(values)
  17. export_dict_to_csv(bank_dict, callCert+'.csv')
ss2ws0br

ss2ws0br1#

  1. import os, glob, pandas as pd, xbrl, csv
  2. from xbrl import XBRLParser, GAAP, GAAPSerializer
  3. # Create file list using either glob or manually
  4. path = 'P:/Bank Research/Bank Automation/Calls'
  5. file_list = glob.glob(path + "/*.XBRL")
  6. callCert = '57944'
  7. dates = ['063023','123122','123121','123120','123119','123118']
  8. file_list = [f"{path}/Call_Cert{callCert}_{date_item}.XBRL" for date_item in dates]
  9. # Add keys and numbers directly to the dictionary
  10. xbrl_parser = XBRLParser()
  11. bank_dict = {}
  12. for xbrl_file in file_list:
  13. xbrl_document = xbrl_parser.parse(xbrl_file)
  14. custom_obj = xbrl_parser.parseCustom(xbrl_document)
  15. for i in custom_obj():
  16. bank_dict[i[0]] = i[1]
  17. # Note that having the same keys will overwrite the previous value
  18. # to handle such cases you can use the following ideas:
  19. # before adding the key check if it exists in the dictionary
  20. bank_dict.get(i[0], None)
  21. # this returns None when the key doesn't exist
  22. # so using conditions such as
  23. if bank_dict.get(i[0], None) is None:
  24. bank_dict[i[0]] = i[1]
  25. else:
  26. # do something else
  27. pass
  28. export_dict_to_csv(bank_dict, callCert+'.csv')
展开查看全部

相关问题