split string by arbitrary number of white spaces

后端 未结 6 1351
夕颜
夕颜 2021-01-17 11:07

I\'m trying to find the most pythonic way to split a string like

\"some words in a string\"

into single words. string.split(\' \')

6条回答
  •  忘掉有多难
    2021-01-17 11:38

    >>> a = "some words in a string"
    >>> a.split(" ")
    ['some', 'words', 'in', 'a', 'string']
    

    split parameter is not included in the result, so i guess theres something more about your string. otherwise, it should work

    if you have more than one whitespace just use split() without parameters

    >>> a = "some words in a string     "
    >>> a.split()
    ['some', 'words', 'in', 'a', 'string']
    >>> a.split(" ")
    ['some', 'words', 'in', 'a', 'string', '', '', '', '', '']
    

    or it will just split a by single whitespaces

提交回复
热议问题