Do you want the output file to be in the format {...}
(contents of all files combined into one dictionary), or [{...}, {...} ...]
(each file is a list element)?
Depending on your goal:
import json
# --- FORMAT: {...} --- #
result1 = {}
# your input dictionaries
for dct in dicts:
# combine the dictionaries
# if there are duplicate keys, the ones in 'dct' will overwrite those already in 'result'
result1 = {**result1, **dct}
# write the output file
with open('result1.json', 'w') as outfile:
json.dump(result1 , outfile, indent=2)
# --- FORMAT: [{...}, {...} ...] --- #
result2 = []
# your input dictionaries
for dct in dicts:
# append the dictionary to the list
result2.append(dct)
# write the output file
with open('result2.json', 'w') as outfile:
json.dump(result2, outfile, indent=2)
The above gives you two different output files depending on the intended format.
EDIT: To get your input dfs in a list, you can use locals().keys()
in your function. So you can do something like for df in locals().keys(): ...
Putting it all together:
def export_to_json(df_domains_and_ip_addresses_to_add_to_your_allow_list
, df_domains_and_ip_addresses_to_add_to_your_allow_list_for_pcoip
, df_domains_and_ip_addresses_to_add_to_your_allow_list_for_workSpaces_streaming_protocol_wsp_beta
, df_health_check_servers
, df_pcoip_gateway_servers
, df_ip_ranges
, df_wsp_beta_gateway_servers
, df_management_interface_ip_ranges):
dest_file_name = 'WORKSPACES_PORT_REQ_' + str(datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) + '.json'
json_buffer = io.StringIO()
result = {}
dfs = locals().keys()
for df in dfs:
result = {**result, **df}
# Adding dict to json file
result.to_json(json_buffer)
my_bucket.put_object(Key=dest_file_name, Body=json_buffer.getvalue())
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…