python 参数列表的分拆的方法?

Python018

python 参数列表的分拆的方法?,第1张

4.7.4. 参数列表的分拆

另有一种相反的情况: 当你要传递的参数已经是一个列表,但要调用的函数却接受分开一个个的参数值。这时候你要把已有的列表拆开来。例如内建函数 range() 需要要独立的 start,stop 参数。你可以在调用函数时加一个 * 操作符来自动把参数列表拆开:

>>>list(range(3, 6))# normal call with separate arguments

[3, 4, 5]

>>>args = [3, 6]

>>>list(range(*args))# call with arguments unpacked from a list

[3, 4, 5]

以同样的方式,可以使用 ** 操作符分拆关键字参数为字典:

>>>def parrot(voltage, state='a stiff', action='voom'):

... print("-- This parrot wouldn't", action, end=' ')

... print("if you put", voltage, "volts through it.", end=' ')

... print("E's", state, "!")

...

>>>d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}

>>>parrot(**d)

-- This parrot wouldn't VOOM if you put four mil

在一些测试平台对接时或者用例执行时,或多或少会用到Python脚本传参的问题。

test.py脚本

#!/usr/bin/python3

import sys

print ('参数个数为:', len(sys.argv), '个参数。')

print ('参数列表:', str(sys.argv))

print ('脚本名:', str(sys.argv[0]))

print ('第一个参数:', sys.argv[1])

执行python3 test.py arg1 arg2 arg3

参数个数为: 4 个参数。

参数列表: ['test.py', 'arg1', 'arg2', 'arg3']

脚本名: test.py

第一个参数: arg1

test.py脚本

#!/usr/bin/python3

import argparse

# 生成了一个命令行参数的对象

parser = argparse.ArgumentParser(description='Test for argparse')

parser.add_argument('--name', '-n', help='name属性,非必要参数')

parser.add_argument('--year', '-y', help='year 属性,非必要参数,但有默认值', default=2017)

parser.add_argument('--body', '-b', help='body属性,必要参数', required=True)

args = parser.parse_args()

print (args.year,  args.name, args.body)

查看帮助python3 test.py --help

usage: test.py [-h] [--name NAME] [--year YEAR] --body BODY

Test for argparse

optional arguments:

  -h, --help  show this help message and exit

  --name或-n NAME  name属性,非必要参数

  --year或-y YEAR  year属性,非必要参数,但有默认值

  --body或-b BODY  body 属性,必要参数

执行python3 test.py --year 2021 -n robot  --body "are you ok?"

2021 robot are you ok?

以方法2中的test.py脚本为例

python3 test.py --year 2021 --body [\"test\", \"robot\",\"boy\" ]

2021 ["test", "robot", "boy" ]

以方法1中的test.py脚本为例

python3 test.py [\"test\", \"robot\",\"boy\" ]

参数个数为: 2个参数。

参数列表: ['test.py', '[\"test\", \"robot\", \"boy\" ]']

脚本名: test.py

第一个参数: ["test", "robot", "boy" ]

其实此时传入的第一个参数是一个字符,需要转换为列表。

import json

json.loads(sys.argv[1])

test_arg.py脚本

#!/usr/bin/python3

import argparse

import os

# 生成了一个命令行参数的对象

parser = argparse.ArgumentParser(description='Test for argparse')

parser.add_argument('--body', '-b', help='body属性,必要参数', required=True)

args = parser.parse_args()

print (args.body)

command=python3 + '  ' + test_sys.py+ '  '  + args.body

print (command)

str=('command')

result=os.system(str)

test_sys.py脚本

#!/usr/bin/python3

import sys

import json

print ('第一个参数:', sys.argv[1])

print ('列表:', json.loads(sys.argv[1]))

执行python3 test_arg.py --body  [\"test\", \"robot\",\"boy\" ]

python3  test_sys.py  ["test", "robot", "boy" ]

test_sys.py执行报错,转json失败。

还记得我们案例2中,脚本的传入指定参数和实际传入参数嘛?

test_arg.py脚本我们稍微优化下,在传参前先字符替换下。

["test", "robot", "boy" ]转换为[\"test\", \"robot\",\"boy\" ]即可。

command.replace(' " ' , r ' \" ') 添加到command=之后,再次运行看看呢?