Python Forum
Passing List of Objects in Command Line Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing List of Objects in Command Line Python
#1
I am trying to pass a argument to command line for running a python program which takes list of Field Objects and send an email. The program is running fine when the list of objects is already set in my program.


a=[{"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"}]
sendmail(a)
But when I pass this list in command line ,it did not work for me.
a = json.loads(sys.argv[1])
sendmail(a)
Running Program from Command Line
mailscript.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"}]'
Error : json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Python Version :3.8.5
Reply
#2
Show us how you're passing the JSON on the command line and tell us which OS you're using. You possibly need to quote it or something.
Reply
#3
I have a added a Single Quote but it also did not work.

I am using Windows
Reply
#4
Apologies; I didn't read your post properly. Hmm, I'm not sure about Windows I'm afraid. Perhaps look up how you're meant to quote things on the command line there.
Reply
#5
Have to do some converting stuff for this to work,there is no easy fix with quotes or anything else.
Prepare command to use in command line.
lst = [{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}]
command_lst = list(lst[0].items())
print(f'"{command_lst}"')
Output:
"[('name', 'productname_0'), ('type', 'html'), ('content', 'A Cool Shirt')]"
# sys_a.py
import sys, ast
import json

arg_command = sys.argv[1]
a_eval = ast.literal_eval(arg_command)
d_arg = dict(a_eval)
lst = []
lst.append(d_arg)
print(lst)

# Test json
j = json.dumps(lst)
# print(j)
load_j = json.loads(j)
print(load_j)
Test command line:
Output:
C:\code λ python sys_a.py "[('name', 'productname_0'), ('type', 'html'), ('content', 'A Cool Shirt')]" [{'name': 'productname_0', 'type': 'html', 'content': 'A Cool Shirt'}] [{'name': 'productname_0', 'type': 'html', 'content': 'A Cool Shirt'}]
Reply
#6
Thanks for the reply,
What should I do for multiple JSON objects for productname_1,product_name2?
Reply
#7
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
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'}]
Reply
#8
That isn't valid JSON though. I'd be very surprised if you couldn't escape quotes in Windows to let you do what you want. Then again, I'm not a Windows user, so..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing varying command line arguements Rakshan 3 169 Jul-28-2021, 03:18 PM
Last Post: snippsat
  passing php variable to python file jerald 1 144 Jul-07-2021, 11:46 AM
Last Post: Larz60+
  Passing flags to python script, through a function xbit 4 633 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  How to input & output parameters from command line argument shantanu97 1 550 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 547 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Select correct item from list for subprocess command pythonnewbie138 6 933 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python if command for a list fid 3 761 May-08-2020, 11:52 AM
Last Post: fid
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 918 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Taking Multiple Command Line Argument Input bwdu 6 1,447 Mar-29-2020, 05:52 PM
Last Post: buran
  Python 2.7 passing variables from functions zetto33 1 632 Mar-19-2020, 07:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020