简体   繁体   English

如何将列表作为命令行参数传递给 python 脚本

[英]how to pass a list as a command line argument to a python script

I have a script - script.py where i am parsing a json file(the json file has a list of directories to copy to remote host) and the json parsing returns me 2 lists - list1 and list2.我有一个脚本 - script.py ,我正在解析 json 文件(json 文件有一个要复制到远程主机的目录列表)和 json 解析返回我 2 个列表和列表。 I am then passing this list1 and list2 to another script script2 that is called inside script.py然后,我将此 list1 和 list2 传递给另一个脚本script2 ,该脚本在script.py中调用

Something like就像是

#!/usr/bin/python
json = 'jsonfile.json'
input_dir='some path1'
output_dir='some path2'
# function to parse json file and get the lists
# call script2.py
path_to_script2/script2.py list1 list2 input_dir output_dir 

I tried我试过了

eg, script2.py list1 list2 input_path output_path This says i cannot pass list type to the script例如, script2.py list1 list2 input_path output_path这表示我无法将列表类型传递给脚本

script2.py has script2.py 有

import os
import sys
import ast

a = ast.literal_eval(sys.argv[1])
b = ast.literal_eval(sys.argv[2])
c = sys.argv[3]
d = sys.argv[4]

print(a)
print(b)

when i try this当我尝试这个

script2.py "list1" "list2" input_path output_path - 

I am getting this error-我收到这个错误-

Traceback (most recent call last):
  File "path/2/script/bexec.p", line 7, in <module>
    a = ast.literal_eval(sys.argv[1])
  File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

Here is the json file that i am parsing-这是我正在解析的 json 文件-

{
"list1": ["./xyz1/abc1/file1.sql",
          "./xyz2/abc2/file2.sql",
          "./xyz3/abc3/file3.sql"
          ],

"list2":[
        {"inp": "./xyz03/abc01/file1.txt",
         "csv": ["./xyz03/abc01/file2.csv"],
          "sql": ["./xyz03/file3.sql"],
          "dat": ["./xyz03/Model/file4.dat"],
       },
       { "inp": "./xyz03/abc01/file2.txt",
         "csv": ["./xyz03/abc01/file2.csv"],
         "sql": ["./xyz03/abc01/file3.sql"],
          "dat": ["./xyz03/Model/file4.dat"]}
        ]
}

This is just a snippet of the json file- It has more values in list2 field这只是 json 文件的片段 - 它在list2字段中有更多值

ast.literal_eval() converts strings in Python literal syntax to the actual objects. ast.literal_eval()将 Python 文字语法中的字符串转换为实际对象。 It might be sufficient for what you want:它可能足以满足您的需求:

import sys
import ast

a = ast.literal_eval(sys.argv[1])
b = ast.literal_eval(sys.argv[2])
c = sys.argv[3]
d = sys.argv[4]

print(type(a),a)
print(type(b),b)
print(c)
print(d)

Use case:用例:

C:\>script.py [1,2,3,4] {'key1':1,'key2':2} input.txt output.txt
<class 'list'> [1, 2, 3, 4]
<class 'dict'> {'key1': 1, 'key2': 2}
input.txt
output.txt

Note if you have spaces in your arguments your shell will required quoting the arguments or escaping the spaces.请注意,如果您的 arguments 中有空格,您的 shell 将需要引用 arguments 或 Z534F19BE86FF67166BBF80B0CDBC11CAA5BDA99F77E7DZ 空格。

C:\>script.py [1,2,3,4] "{'key with spaces':1,'key2':2}" input.txt output.txt
<class 'list'> [1, 2, 3, 4]
<class 'dict'> {'key with spaces': 1, 'key2': 2}
input.txt
output.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM