Don't split double-quoted words with Python string split()?

前端 未结 3 1570
感动是毒
感动是毒 2020-12-20 13:17

When using the Python string function split(), does anybody have a nifty trick to treat items surrounded by double-quotes as a non-splitting word?

Say I want to spli

相关标签:
3条回答
  • 2020-12-20 13:31

    I suggest you search with re for the pattern "[^"]*" and apply string.split only on the remaining parts. You could implement a recursive function that processes all relevant string parts.

    0 讨论(0)
  • 2020-12-20 13:37

    @Rob: why without regexes if the regexp solution is so simple?

    my_str = 'A B\t"C" DE "FE"\t\t"GH I JK L" "" ""\t"O P   Q" R'
    print re.findall(r'(\w+|".*?")', my_str)
    ['A', 'B', '"C"', 'DE', '"FE"', '"GH I JK L"', '""', '""', '"O P   Q"', 'R']
    
    0 讨论(0)
  • 2020-12-20 13:42

    You won't be able to get this behaviour with str.split(). If you can live with the rather complex parsing it does (like ignoring double quotes preceded by a back slash), shlex.split() might be what you are looking for:

    >>> shlex.split(myStr)
    ['A', 'B', 'C', 'DE', 'FE', 'GH I JK L', '', '', 'O P   Q', 'R']
    
    0 讨论(0)
提交回复
热议问题