在命令行Python中传递对象列表

在命令行Python中传递对象列表,python,Python,我试图将一个参数传递给命令行以运行python程序,该程序获取字段对象列表并发送电子邮件 当我的程序中已经设置了对象列表时,程序运行正常 a=[{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}, {"name": "productname_1", &q

我试图将一个参数传递给命令行以运行python程序,该程序获取字段对象列表并发送电子邮件

当我的程序中已经设置了对象列表时,程序运行正常

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)
但当我在命令行中传递这个列表时,它对我不起作用

a =sys.argv[1]
sendmail(a)
从命令行运行程序

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"}]

在程序内部使用:

a = json.loads(sys.argv[1])
sendmail(a)
按以下方式执行程序:

python 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"}]'

在程序内部使用:

a = json.loads(sys.argv[1])
sendmail(a)
按以下方式执行程序:

python 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"}]'


您是否尝试过
python mailscript.py[{“name”:“productname_0”,“type”:“html”,“content”:“一件酷衬衫”},{“name”:“productname_1”,“type”:“html”,“content”:“一些漂亮的鞋子”},{“name”:“productname_2”,“type”:“html”,“content”:“一顶时髦的帽子”},{“name”:“productprice_0”,“type”:“html”,“content”:“20.99”},{“name”:“productprice_1”,“type”:“html”,“content”:“50.99”},{“name”:“productprice_2”,“type”:“html”,“content”:“FREE”}]
?传递的参数始终是字符串。在
argv
中没有列表,因为输入看起来像一个列表。您需要首先将字符串作为列表进行排序。例如,您可能需要执行一些quoting.escaoping“和>在命令行中-首先打印传递的参数,并对引号进行排序,使打印输出为所需的字符串,然后将其解析排序为实际的lPython列表。使用argv获取值的类型为
str
(您可以使用
type
进行检查)在这种情况下,str可以使用
json.loads
解析回dict列表,因为您的数据只使用双引号,所以您可以使用单引号来引用整个内容:
mailscript.py'[{…}]'
。您是否尝试过
python mailscript.py[{“name”:“productname\u 0”,“类型”:“html”,“内容”:“一件酷衬衫”},{“名称”:“产品名称”;“内容”:“一些漂亮的鞋子”},{“名称”:“产品名称”;“类型”:“html”,“内容”:“一顶时髦的帽子”},{“名称”:“产品价格”;“类型”:“html”,“内容”:“20.99”},{“名称”:“产品价格”;“类型”:“html”,“内容”:“50.99”;{“名称”:“产品价格”;“类型”“:“html”,“content:“FREE”}]
?传递的参数始终是字符串。您在
argv
中无法获得列表,因为输入看起来像一个列表。您需要首先将字符串作为一个列表。您可能需要进行一些引用。例如“和>在命令行中-首先打印传递的参数,并对引号进行排序,使打印输出为所需的字符串,然后将其解析排序为实际的lPython列表。使用argv获取值的类型为
str
(您可以使用
type
进行检查)在这种情况下,str可以使用
json.loads
解析回dict列表,因为您的数据只使用双引号,所以您可以使用单引号引用整个内容,如下所示:
mailscript.py'[{…}]“
。我尝试了这个方法,但它显示的是json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)您的python版本是什么?python版本:3.8.5如果您是从PyCharm执行它,那么从PyCharm中出来并从命令行执行它。您使用的是哪一个python版本?我试过了,但它说的是json.decoder.JSONDecodeError:期望值:第1行第1列(char 0)您的python版本是什么?python版本:3.8.5如果您是从PyCharm执行它,那么从它出来并从命令行执行它。您使用的是哪个python版本?