Python shlex.split(), ignore single quotes

后端 未结 2 1124
小蘑菇
小蘑菇 2021-02-08 14:22

How, in Python, can I use shlex.split() or similar to split strings, preserving only double quotes? For example, if the input is \"hello, world\" is what \'i

2条回答
  •  离开以前
    2021-02-08 15:19

    You can use shlex.quotes to control which characters will be considered string quotes. You'll need to modify shlex.wordchars as well, to keep the ' with the i and the say.

    import shlex
    
    input = '"hello, world" is what \'i say\''
    lexer = shlex.shlex(input)
    lexer.quotes = '"'
    lexer.wordchars += '\''
    
    output = list(lexer)
    # ['"hello, world"', 'is', 'what', "'i", "say'"]
    

提交回复
热议问题