【问题】
别人问的问题:
主题:parser.parse_args() 不能识别带&的字符串,请教如何解决,谢谢! 消息正文:
在Windows下输入:
报错: Namespace(url=’http://tieba.baidu.com%ere’) ‘2’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 |
【解决过程】
1.自己去测试测试看看。
然后测试代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/usr/bin/python # -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Function: 【已解决】测试Python 3.x中的argparse是否支持输入参数中包含花at符号(ampersand,'&') Author: Crifan Li Verison: 2013-11-25 Contact: https://www.crifan.com/about/me ------------------------------------------------------------------------------- """ import argparse def argparse_ampersand(rawValue): """test python 3.x argparse parse input para contain ampersand '&' """ parser = argparse.ArgumentParser(description = "test python 3.x argparse parse input para contain ampersand '&'" ) parser.add_argument( '-u' , type = url, required = True , metavar = 'URL' , dest = 'url' , help = 'Specify the begin url' ) print ( "test python 3.x argparse parse input para contain ampersand '&'" ) args = parser.parse_args() print ( "parsed input args=" , args); if __name__ = = "__main__" : argparse_ampersand(); |
测试结果是:
F:\argparse_ampersand>argparse_ampersand.py File "F:\argparse_ampersand\argparse_ampersand.py", line 25, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere&2 Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) ‘2’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere\&2 Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) ‘2’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere\\&2 Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) ‘2’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere’&’2 Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) ”2′ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 F:\argparse_ampersand> |
如图:
即:
还是无法直接输入带&的字符,即使加了反斜杠等转义字符,也不行。
2.参考:
How can I recognise the argv correctly in python cmd?
去给参数值,加上双引号:
结果是:
F:\argparse_ampersand>argparse_ampersand.py -u "http://tieba.baidu.com%ere&2" Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) F:\argparse_ampersand>argparse_ampersand.py "-u http://tieba.baidu.com%ere&2" Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) F:\argparse_ampersand>argparse_ampersand.py "-u" "http://tieba.baidu.com%ere&2" Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) F:\argparse_ampersand>argparse_ampersand.py "-u" "http://tieba.baidu.com%ere&2" Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere^&2 Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 26, in <module> argparse_ampersand(); TypeError: argparse_ampersand() takes exactly 1 argument (0 given) F:\argparse_ampersand>python argparse_ampersand.py -u http://tieba.baidu.com%ere^&2 ‘python’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 F:\argparse_ampersand> |
即:
貌似,是可以了。
只是出现了其他的:
TypeError: xxx takes exactly 1 argument (0 given) |
的错误。
3.所以参考我自己之前的代码:
改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/usr/bin/python # -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Function: 【已解决】测试Python 3.x中的argparse是否支持输入参数中包含花at符号(ampersand,'&') Author: Crifan Li Verison: 2013-11-25 Contact: https://www.crifan.com/about/me ------------------------------------------------------------------------------- """ import argparse def argparse_ampersand(rawValue): """test python 3.x argparse parse input para contain ampersand '&' """ parser = argparse.ArgumentParser(description = "test python 3.x argparse parse input para contain ampersand '&'" ) newParser.add_argument( "-u" , "--url" , dest = "url" , help = "Specify the begin url" ); #parser.add_argument('-u', type=url, required=True, metavar='URL', dest='url', help='Specify the begin url') print ( "test python 3.x argparse parse input para contain ampersand '&'" ) args = parser.parse_args() print ( "parsed input args=" , args); if __name__ = = "__main__" : argparse_ampersand(); |
再去试试:
结果问题依旧。
4.后来发现貌似是print的少了格式化的%s,加上后,变成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/usr/bin/python # -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Function: 【已解决】测试Python 3.x中的argparse是否支持输入参数中包含花at符号(ampersand,'&') Author: Crifan Li Verison: 2013-11-25 Contact: https://www.crifan.com/about/me ------------------------------------------------------------------------------- """ import argparse def argparse_ampersand(rawValue): """test python 3.x argparse parse input para contain ampersand '&' """ parser = argparse.ArgumentParser(description = "test python 3.x argparse parse input para contain ampersand '&'" ) newParser.add_argument( "-u" , "--url" , dest = "url" , help = "Specify the begin url" ); #parser.add_argument('-u', type=url, required=True, metavar='URL', dest='url', help='Specify the begin url') print ( "test python 3.x argparse parse input para contain ampersand '&'" ) args = parser.parse_args() print ( "parsed input args=%s" , args); if __name__ = = "__main__" : argparse_ampersand(); |
再去试试,
问题依旧。
5.后来才发现,原来是:
line 27,即27行的
argparse_ampersand();
出错的。。。
对应的定义是:
def argparse_ampersand(rawValue):
所以是:
笔误,弄出个多余的参数。。。而搞的鬼。
所以改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/usr/bin/python # -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Function: 【已解决】测试Python 3.x中的argparse是否支持输入参数中包含花at符号(ampersand,'&') Author: Crifan Li Verison: 2013-11-25 Contact: https://www.crifan.com/about/me ------------------------------------------------------------------------------- """ import argparse def argparse_ampersand(): """test python 3.x argparse parse input para contain ampersand '&' """ parser = argparse.ArgumentParser(description = "test python 3.x argparse parse input para contain ampersand '&'" ) #newParser.add_argument("-u", "--url", dest="url", help="Specify the begin url"); parser.add_argument( '-u' , type = url, required = True , metavar = 'URL' , dest = 'url' , help = 'Specify the begin url' ) print ( "test python 3.x argparse parse input para contain ampersand '&'" ) args = parser.parse_args() print ( "parsed input args=" , args); if __name__ = = "__main__" : argparse_ampersand(); |
再去试试:
结果又出现了:
F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere^&2 Traceback (most recent call last): File "F:\argparse_ampersand\argparse_ampersand.py", line 27, in <module> argparse_ampersand(); File "F:\argparse_ampersand\argparse_ampersand.py", line 21, in argparse_ampersand parser.add_argument(‘-u’, type=url, required=True, metavar=’URL’, dest=’url’, help=’Specify the begin url’) NameError: global name ‘url’ is not defined |
6.再去改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/usr/bin/python # -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Function: 【已解决】测试Python 3.x中的argparse是否支持输入参数中包含花at符号(ampersand,'&') Author: Crifan Li Verison: 2013-11-25 Contact: https://www.crifan.com/about/me ------------------------------------------------------------------------------- """ import argparse def argparse_ampersand(): """test python 3.x argparse parse input para contain ampersand '&' """ parser = argparse.ArgumentParser(description = "test python 3.x argparse parse input para contain ampersand '&'" ) parser.add_argument( "-u" , "--url" , dest = "url" , help = "Specify the begin url" ); #parser.add_argument('-u', type=url, required=True, metavar='URL', dest='url', help='Specify the begin url') print ( "test python 3.x argparse parse input para contain ampersand '&'" ) args = parser.parse_args() print ( "parsed input args=" , args); if __name__ = = "__main__" : argparse_ampersand(); |
结果是:
F:\argparse_ampersand>argparse_ampersand.py -u http://tieba.baidu.com%ere^&2 test python 3.x argparse parse input para contain ampersand ‘&’ parsed input args= Namespace(url=’http://tieba.baidu.com%ere&2′) F:\argparse_ampersand>argparse_ampersand.py -u "http://tieba.baidu.com%ere&2" test python 3.x argparse parse input para contain ampersand ‘&’ parsed input args= Namespace(url=’http://tieba.baidu.com%ere&2′) F:\argparse_ampersand>argparse_ampersand.py -u ‘http://tieba.baidu.com%ere&2′ test python 3.x argparse parse input para contain ampersand ‘&’ parsed input args= Namespace(url="’http://tieba.baidu.com%ere") ‘2” 不是内部或外部命令,也不是可运行的程序 或批处理文件。 F:\argparse_ampersand>argparse_ampersand.py "-u http://tieba.baidu.com%ere&2" test python 3.x argparse parse input para contain ampersand ‘&’ parsed input args= Namespace(url=’ http://tieba.baidu.com%ere&2′) |
如图:
【总结】
Python 3.x中,使用argparse去解析输入参数,其中包含&的话:
有两种解决办法:
1.可以在单个的&前面加上上尖括号’^’去转义&,即可:
argparse_ampersand.py -u http://tieba.baidu.com%ere^&2 |
2.或者是,给整个单个参数加上双引号,也可以的:
argparse_ampersand.py -u "http://tieba.baidu.com%ere&2" |
注:
1.加单引号:
argparse_ampersand.py -u ‘http://tieba.baidu.com%ere&2‘ |
是不行的。
2.给整个参数部分加上双引号:
argparse_ampersand.py "-u http://tieba.baidu.com%ere&2" |
虽然是可以,但是会导致解析后的值,是包含空格的:
Namespace(url=’ http://tieba.baidu.com%ere&2′) |
与此对应的,正常的值是没空格的:
Namespace(url=’http://tieba.baidu.com%ere&2′) |
所以还是不推荐整个参数部分加双引号,还是单独给包含特殊字符(此处的&)的参数去加双引号就行了。
转载请注明:在路上 » 【已解决】测试Python 3.x中的argparse是否支持输入参数中包含花at符号(ampersand,’&’)