本文介绍了如何遍历参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的脚本:
import argparse
parser = argparse.ArgumentParser(
description='Text file conversion.'
)
parser.add_argument("inputfile", help="file to process", type=str)
parser.add_argument("-o", "--out", default="output.txt",
help="output name")
parser.add_argument("-t", "--type", default="detailed",
help="Type of processing")
args = parser.parse_args()
for arg in args:
print(arg)
但是它不起作用.我收到错误消息:
But it doesnt work. I get error:
TypeError: 'Namespace' object is not iterable
如何遍历参数及其值?
推荐答案
如果要遍历名称空间对象,请添加'vars':
Please add 'vars' if you wanna iterate over namespace object:
for arg in vars(args):
print arg, getattr(args, arg)
这篇关于如何遍历参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!