Sep-27-2020, 03:44 PM
I will be like this,not so easy to pass all this trough command line Windows.
There may be a better way if i had use Click,and maybe pass a file object instead.
Make argument
There may be a better way if i had use Click,and maybe pass a file object instead.
Make argument
lst = [{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}, {"name": "productname_1", "type": "html", "content": "Some Nice Shoes"}, {"name": "productname_2", "type": "html", "content": "A Trendy Hat"}, {"name": "productprice_0", "type": "html", "content": "20.99"}, {"name": "productprice_1", "type": "html", "content": "50.99"}, {"name": "productprice_2", "type": "html", "content": "FREE"}] command_lst = [] for index,item in enumerate(lst): command_lst.append(list(lst[index].items())) print(f'"{command_lst}"')
# command_arg.py import sys, ast import json from pprint import pprint arg_command = sys.argv[1] a_eval = ast.literal_eval(arg_command) lst = [] for i in a_eval: d_arg = dict(i) lst.append(d_arg) print(lst) # Test json j = json.dumps(lst) # print(j) load_j = json.loads(j) pprint(load_j)Command line:
Output:C:\code\json_commandline
λ python command_arg.py "[[('name', 'productname_0'), ('type', 'html'), ('content', 'A Cool Shirt')], [('name', 'productname_1'), ('type', 'html'), ('content', 'Some Nice Shoes')], [('name', 'productname_2'), ('type', 'html'), ('content', 'A Trendy Hat')], [('name', 'productprice_0'), ('type', 'html'), ('content', '20.99')], [('name', 'productprice_1'), ('type', 'html'), ('content', '50.99')], [('name', 'productprice_2'), ('type', 'html'), ('content', 'FREE')]]"
[{'name': 'productname_0', 'type': 'html', 'content': 'A Cool Shirt'}, {'name': 'productname_1', 'type': 'html', 'content': 'Some Nice Shoes'}, {'name': 'productname_2', 'type': 'html', 'content': 'A Trendy Hat'}, {'name': 'productprice_0', 'type': 'html', 'content': '20.99'}, {'name': 'productprice_1', 'type': 'html', 'content': '50.99'}, {'name': 'productprice_2', 'type': 'html', 'content': 'FREE'}] [{'content': 'A Cool Shirt', 'name': 'productname_0', 'type': 'html'},
{'content': 'Some Nice Shoes', 'name': 'productname_1', 'type': 'html'},
{'content': 'A Trendy Hat', 'name': 'productname_2', 'type': 'html'},
{'content': '20.99', 'name': 'productprice_0', 'type': 'html'},
{'content': '50.99', 'name': 'productprice_1', 'type': 'html'},
{'content': 'FREE', 'name': 'productprice_2', 'type': 'html'}]