shlex模塊實現了一個類來解析簡單的類shell語法,可以用來編寫領域特定的語言,或者解析加引號的字符串。
處理輸入文本時有一個常見的問題,往往要把一個加引號的單詞序列標識爲一個實體。根據引號劃分文本可能與預想的並不一樣,特別是嵌套有多層引號時。例:
有文本quotes.txt,內容如下
This string has embedded "double quotes" and 'single quotes' in it,
and even "a 'nested example'".
一種簡單的方法是構造一個正則表達式,來查找引號之外的文本部分,將它們與引號內的文本分開,或者反之。這可能帶來不必要的複雜性,而且很容易因爲邊界條件出錯,如撇號或者拼寫錯誤。更好地解決方案是使用一個真正的解析器,如shlex模塊提供的解析器。以下是一個簡單的例子,它使用shlex類打印輸入文件中找到的token。
- #!/usr/bin/python
- import shlex
- import sys
- if len(sys.argv) != 2:
- print 'Please specify one filename on the command line.'
- sys.exit(1)
- filename = sys.argv[1]
- body = file(filename, 'rt').read()
- print 'ORIGINAL:', repr(body)
- print 'TOKENS:'
- lexer = shlex.shlex(body)
- for token in lexer:
- print repr(token)
執行 python shlex_example.py quotes.txt
結果
ORIGINAL: 'This string has embedded "double quotes" and \'single quotes\' in it,\nand even "a \'nested example\'".\n'
TOKENS:
'This'
'string'
'has'
'embedded'
'"double quotes"'
'and'
"'single quotes'"
'in'
'it'
','
'and'
'even'
'"a \'nested example\'"'
'.'
另外,孤立的引號(如I'm)也會處理。看以下文件
This string has an embedded apostrophe, doesn't it?
用shlex完全可以找出包含嵌入式撇號的token
執行 python shlex_example.py apostrophe.txt
結果: