【问题】
在:
的折腾过程中,想要实现,将使用argparse解析后的命令行的参数,像使用optparse解析后的参数一样,去导出这些变量,使得后面的代码,可以直接用于参数值。
相关的,使用optparse解析并导出变量的代码为:
# 2. 演示optparse的用法 oldParser = OptionParser(); oldParser.add_option("-u","--username",action="store", type="string",dest="username_optparse",help="Your user name"); oldParser.add_option("-a","--age",action="store", type="int",dest="age_optparse",help="Your age"); (options, args) = oldParser.parse_args(); #此处用于导出所获得的变量 #如果没有导出变量,则后面代码中,如果用到此处定义的参数所对应的变量时,就会报错,说找不到相关的变量 #NameError: global name 'username_optparse' is not defined for i in dir(options): exec(i + " = options." + i); print "optparse: Your input username_optparse=%s, type(username_optparse)=%s, age_optparse=%d, type(age_optparse)=%s"%(username_optparse, type(username_optparse), age_optparse, type(age_optparse));
使用argparse可以解析变量:
newParser = argparse.ArgumentParser(); newParser.add_argument("-u", "--username", dest="username_argparse", help="Your user name"); newParser.add_argument("-a", "--age", type=int, dest="age_argparse", help="Your age"); args = newParser.parse_args();
后续,已经是可以通过args.username_argparse和args.age_argparse去使用对应的参数了。
但是想要实现,后续代码可以直接此处的username_argparse和age_argparse。
即,需要像上面的optparse中一样,通过exec去export对应的变量一样。
【解决过程】
1.此处,先去研究args的变量,最终搞懂了,是对应的namespace类型:
newParser = argparse.ArgumentParser(); newParser.add_argument("-u", "--username", dest="username_argparse", help="Your user name"); newParser.add_argument("-a", "--age", type=int, dest="age_argparse", help="Your age"); args = newParser.parse_args(); print "args=",args; #args= Namespace(age_argparse=1000, username_argparse='crifanLi') print "type(args)=",type(args); #type(args)= <class 'argparse.Namespace'>
然后想要想办法,找到其中的age_argparse和username_argparse,然后后续的折腾,就是:
【已解决】python中枚举(enumerate/iterate)域名(namespace)变量
然后,得到了字典类型的argsDict变量后,接着就是想办法,把变量的key和value,像optparse一样,去用exec去导出了。
2.结果是,先通过:
newParser = argparse.ArgumentParser(); newParser.add_argument("-u", "--username", dest="username_argparse", help="Your user name"); newParser.add_argument("-a", "--age", type=int, dest="age_argparse", help="Your age"); args = newParser.parse_args(); print "args=",args; #args= Namespace(age_argparse=1000, username_argparse='crifanLi') print "type(args)=",type(args); #type(args)= <class 'argparse.Namespace'> argsDict = args.__dict__; print "parsed argsDict=",argsDict; #parsed argsDict= {'age_argparse': 1000, 'username_argparse': 'crifanLi'} for eachArg in argsDict.keys(): exec(eachArg + " = " + argsDict[eachArg]);
遇到了:
TypeError: cannot concatenate ‘str’ and ‘int’ objects
的错误:
Traceback (most recent call last): File "D:\tmp\tmp_dev_root\python\parse_cmd_para\parse_command_line_para.py", line 108, in <module> cmdParaDemo(); File "D:\tmp\tmp_dev_root\python\parse_cmd_para\parse_command_line_para.py", line 61, in cmdParaDemo exec(eachArg + " = " + argsDict[eachArg]); TypeError: cannot concatenate 'str' and 'int' objects
2.再通过:
newParser = argparse.ArgumentParser(); newParser.add_argument("-u", "--username", dest="username_argparse", help="Your user name"); newParser.add_argument("-a", "--age", type=int, dest="age_argparse", help="Your age"); args = newParser.parse_args(); print "args=",args; #args= Namespace(age_argparse=1000, username_argparse='crifanLi') print "type(args)=",type(args); #type(args)= <class 'argparse.Namespace'> argsDict = args.__dict__; print "parsed argsDict=",argsDict; #parsed argsDict= {'age_argparse': 1000, 'username_argparse': 'crifanLi'} # for eachArg in argsDict.keys(): # exec(eachArg + " = " + argsDict[eachArg]); #TypeError: cannot concatenate 'str' and 'int' objects for eachArg in argsDict.keys(): if(type(argsDict[eachArg]) != "<type 'string'>"): exec(eachArg + " = " + str(argsDict[eachArg])); else: exec(eachArg + " = " + argsDict[eachArg]);
出现了:
NameError: name ‘crifanLi’ is not defined
的错误:
Traceback (most recent call last): File "D:\tmp\tmp_dev_root\python\parse_cmd_para\parse_command_line_para.py", line 108, in <module> cmdParaDemo(); File "D:\tmp\tmp_dev_root\python\parse_cmd_para\parse_command_line_para.py", line 65, in cmdParaDemo exec(eachArg + " = " + str(argsDict[eachArg])); File "<string>", line 1, in <module> NameError: name 'crifanLi' is not defined
3.最后经过一堆的折腾,终于搞定了。
这样写就可以了:
# 3. 演示argparse的用法 newParser = argparse.ArgumentParser(); newParser.add_argument("-u", "--username", dest="username_argparse", help="Your user name"); newParser.add_argument("-a", "--age", type=int, dest="age_argparse", help="Your age"); args = newParser.parse_args(); print "args=",args; #args= Namespace(age_argparse=1000, username_argparse='crifanLi') print "type(args)=",type(args); #type(args)= <class 'argparse.Namespace'> argsDict = args.__dict__; print "parsed argsDict=",argsDict; #parsed argsDict= {'age_argparse': 1000, 'username_argparse': 'crifanLi'} # for eachArg in argsDict.keys(): # exec(eachArg + " = " + argsDict[eachArg]); #TypeError: cannot concatenate 'str' and 'int' objects # for eachArg in argsDict.keys(): # if(type(argsDict[eachArg]) != "<type 'string'>"): # exec(eachArg + " = " + str(argsDict[eachArg])); #NameError: name 'crifanLi' is not defined # else: # exec(eachArg + " = " + argsDict[eachArg]); for eachArg in argsDict.keys(): exec(eachArg + " = args." + eachArg); print "argparse: Your input username_argparse=%s, type(username_argparse)=%s, age_argparse=%d, type(age_argparse)=%s"%(username_argparse, type(username_argparse), age_argparse, type(age_argparse));
正常输出为:
parsed argsDict= {'age_argparse': 1000, 'username_argparse': 'crifanLi'} argparse: Your input username_argparse=crifanLi, type(username_argparse)=<type 'str'>, age_argparse=1000, type(age_argparse)=<type 'int'>
【总结】
看来,之前optparse中通过
for i in dir(options): exec(i + " = options." + i);
而可以正常导出变量的原因在于,其中的options,就是和argparse中的args一样,都是一个namespace,所以,最终通过
namespace.someKey
的方式,可以通过exec最终得到赋值,由此实现变量导出的目的。
转载请注明:在路上 » 【已解决】Python中,如何导出argparse解析后的namespace中的变量(export argparse parsed argument)