Python - line splitting with default delimiter and user-defined delimiter

I tried a simple line-splitting example, but I get some unexpected behavior. Here is a sample code:

def split_string(source,splitlist): for delim in splitlist: source = source.replace(delim, ' ') return source.split(' ') out = split_string("This is a test-of the,string separation-code!", " ,!-") print out >>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code', ''] 

As you can see, I got an extra blank line at the end of the list when I use space as a separator argument for the split () function. However, if I do not pass a single argument to the split () function, I did not get an empty line at the end of the output list.

From what I read in python docs, they said that the default argument to split () is a space. So why, when I explicitly pass "as a delimiter", does it create an empty line at the end of the output list?

+6
source share
2 answers

docs :

If sep is not specified or None, a different splitting algorithm is used: the runs of consecutive spaces are considered as single separators, and the result will not contain empty lines at the beginning or end if the line has leading or trailing spaces.

+12
source

This can happen if you have several spaces separating two words. For instance,

 'a b'.split(' ') will return ['a', '', '', '', 'b'] 

But I would suggest you use split from re module. See an example below:

 import re print re.split('[\s,; !]+', 'ab !!!!!!! , hello ;;;;; world') 

When we run the above snippet, it outputs ['a', 'b', 'hello', 'world']

-2
source

Source: https://habr.com/ru/post/987320/


All Articles