16

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

"some words in a string"

into single words. string.split(' ') works ok but it returns a bunch of white space entries in the list. Of course i could iterate the list and remove the white spaces but I was wondering if there was a better way?

4

6 に答える 6

38

Just use my_str.split() without ' '.


More, you can also indicate how many splits to perform by specifying the second parameter:

>>> ' 1 2 3 4  '.split(None, 2)
['1', '2', '3 4  ']
>>> ' 1 2 3 4  '.split(None, 1)
['1', '2 3 4  ']
于 2012-10-23T10:11:10.180 に答える
15

How about:

re.split(r'\s+',string)

\s is short for any whitespace. So \s+ is a contiguous whitespace.

于 2012-10-23T10:11:30.030 に答える
7

Use string.split() without an argument or re.split(r'\s+', string) instead:

>>> s = 'some words in a string   with  spaces'
>>> s.split()
['some', 'words', 'in', 'a', 'string', 'with', 'spaces']
>>> import re; re.split(r'\s+', s)
['some', 'words', 'in', 'a', 'string', 'with', 'spaces']

From the docs:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

于 2012-10-23T10:11:26.433 に答える
3
>>> 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

于 2012-10-23T10:11:59.817 に答える
1

The most Pythonic and correct ways is to just not specify any delimiter:

"some words in a string".split()

# => ['some', 'words', 'in', 'a', 'string']

Also read: How can I split by 1 or more occurrences of a delimiter in Python?

于 2019-10-05T18:08:42.170 に答える
0
text = "".join([w and w+" " for w in text.split(" ")])

converts large spaces into single spaces

于 2012-11-09T16:00:48.127 に答える