python標準模塊shlex

shlex模塊實現了一個類來解析簡單的類shell語法,可以用來編寫領域特定的語言,或者解析加引號的字符串。

處理輸入文本時有一個常見的問題,往往要把一個加引號的單詞序列標識爲一個實體。根據引號劃分文本可能與預想的並不一樣,特別是嵌套有多層引號時。例:

有文本quotes.txt,內容如下

This string has embedded "double quotes" and 'single quotes' in it,

and even "a 'nested example'".

一種簡單的方法是構造一個正則表達式,來查找引號之外的文本部分,將它們與引號內的文本分開,或者反之。這可能帶來不必要的複雜性,而且很容易因爲邊界條件出錯,如撇號或者拼寫錯誤。更好地解決方案是使用一個真正的解析器,如shlex模塊提供的解析器。以下是一個簡單的例子,它使用shlex類打印輸入文件中找到的token。

  1. #!/usr/bin/python 
  2.  
  3. import shlex 
  4. import sys 
  5.  
  6. if len(sys.argv) != 2
  7.     print 'Please specify one filename on the command line.' 
  8.     sys.exit(1
  9.  
  10. filename = sys.argv[1
  11. body = file(filename, 'rt').read() 
  12. print 'ORIGINAL:', repr(body) 
  13. print 
  14.  
  15. print 'TOKENS:' 
  16. lexer = shlex.shlex(body) 
  17. for token in lexer: 
  18.     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

結果:

ORIGINAL: "This string has an edbedded apostrophe, doesn't it?"

TOKENS:
'This'
'string'
'has'
'an'
'edbedded'
'apostrophe'
','
"doesn't"
'it'
'?'
 
 
可以看出shlex非常智能,比正則表達式方便多了。
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章