Python shlex.split(), ignore single quotes

后端 未结 2 1233
小蘑菇
小蘑菇 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:05

    import shlex
    
    def newSplit(value):
        lex = shlex.shlex(value)
        lex.quotes = '"'
        lex.whitespace_split = True
        lex.commenters = ''
        return list(lex)
    
    print newSplit('''This string has "some double quotes" and 'some single quotes'.''')
    

提交回复
热议问题