Problem statement:
This is how I am invoking my prepare_payload.py ,
python3 prepare_payload.py ['Test_B1','Test_B2','Test_B3'] [https://10.5.5.1,https://10.5.5.2,https://10.5.5.3] ['abc','efg','sss']
my json payload which I am trying to prepare:
{
"billing_account_number": "",
"vcenter_url": "",
"cred_header": "",
}
Expected output:
{
"billing_account_number": "Test_B1",
"vcenter_url": "https://10.5.5.1",
"cred_header": "abc",
}
{
"billing_account_number": "Test_B2",
"vcenter_url": "https://10.5.5.2",
"cred_header": "efg",
}
{
"billing_account_number": "Test_B3",
"vcenter_url": "https://10.5.5.3",
"cred_header": "sss",
}
my code:
import json
import os
import sys
master_list = []
billing_account_number = sys.argv[1]
ip_addr = sys.argv[2]
cred_header = sys.argv[3]
res = list(map(str, billing_account_number.strip('[]').split(',')))
ip = list(map(str, ip_addr.strip('[]').split(',')))
cred_headers = list(map(str, cred_header.strip('[]').split(',')))
master_list.append(res)
master_list.append(ip)
master_list.append(cred_headers)
def prepare_payload():
with open("rabbitmq_payload.json") as fh:
data = json.load(fh)
print('================================================')
return data
data = prepare_payload()
for i in master_list:
for j in i:
data['billing_account_number'] = j
data['vcenter_url'] = j
data['cred_header'] = j
print(data)
I am not able to figure if I have to merge these individual list such as res, IP, cred_headers into a single list and then iterate like main list [res[0],IP[0],cred_headers[0]] and then start replacing key value pair in my data dictionary? Please help me if there is any built in function I can rely on or any efficient approach to solve this problem. Thank you in advance for all the awesome python coders!
CodePudding user response:
It's kind of inconvenient to pass in lists as command line arguments. Better to use standard input, but nonetheless, take a look at this code:
import sys
import ast
billing_account_number = ast.literal_eval(sys.argv[1])
ip_addr = ast.literal_eval(sys.argv[2])
cred_header = ast.literal_eval(sys.argv[3])
output = []
for i in range(len(billing_account_number)):
output.append({"billing_account_number": billing_account_number[i], "ip_addr": ip_addr[i], "cred_header": cred_header[i]})
print(output)
The output:
[{'billing_account_number': 'Test_B1', 'ip_addr': 'https://10.5.5.1', 'cred_header': 'abc'}, {'billing_account_number': 'Test_B2', 'ip_addr': 'https://10.5.5.2', 'cred_header': 'efg'}, {'billing_account_number': 'Test_B3', 'ip_addr': 'https://10.5.5.3', 'cred_header': 'sss'}]
You will need to wrap your command line arguments in quotes so that it parses correctly. Also, this code assumes that all 3 lists are the same length. If that's not guaranteed, then you'll need to iterate over the longest list and set the missing values to empty string.
CodePudding user response:
(It seems like you're asking different questions. How to take argv better is different from how to merge lists. This is for the question in your text. It's also hard to tell what you're trying to do with your file.)
You have content in 3 lists that you want to both merge, and convert to dictionaries.
The builtin way to merge lists is zip()
zip(res, ips, cred_headers)
There are multiple ways to convert this to dictionaries. One is a list comprehension:
result = [{
"billing_account_number": res_item,
"ip_addr": ips_item,
"cred_header": cred_item,
} for res_item, ips_item, cred_item in zip(res, ips, cred_headers)]