PyCon 2011 - Hidden Treasures of the Python Standard Library - 词法分析器分析命令行参数

 

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。


作者:liuyuan_jq

2011-03-30

 

 

 

完整源码

 

#!/usr/bin/env python # encoding: utf-8 # shlex_split.py """Splitting a command line into arguments. """ import shlex for cmd in [ r'''ls "hidden stdlib" "/"already quoted/"" "with ' embedded"''', # subprocess """ls 'hidden stdlib' '"already quoted"' 'with '"'"' embedded'""", # pipes ]: print cmd print cmd.split(' ') print shlex.split(cmd) print

 

 

运行结果

 

 

''ls "hidden stdlib" "/"already quoted/"" "with ' embedded" ["''ls", '"hidden', 'stdlib"', '"//"already', 'quoted//""', '"with', "'", 'embedded"'] ['ls', 'hidden stdlib', '"already quoted"', "with ' embedded"] ls 'hidden stdlib' '"already quoted"' 'with '"'"' embedded' ['ls', "'hidden", "stdlib'", '/'"already', 'quoted"/'', "'with", '/'"/'"/'', "embedded'"] ['ls', 'hidden stdlib', '"already quoted"', "with ' embedded"] 

 

 

 


 

你可能感兴趣的:(PyCon 2011 - Hidden Treasures of the Python Standard Library - 词法分析器分析命令行参数)