Home > Enterprise >  how to keep the whitespace when splitting sentence to a list
how to keep the whitespace when splitting sentence to a list

Time:05-12

below is the code that splits the sentence "s".

s = "1 a 3 bb  b8"
b = s.split()
print(b)

The output from the above code is ['1', 'a', '3', 'bb', 'b8'].

desired output is ['1', 'a', '3', 'bb', ' b8'].

CodePudding user response:

That is a tricky one which make it hard to do with generic function and thus require some custom code.

I took s = s = "1 a 3 bb b8" with 3 white spaces before b8 to make it more fun :)

So first thing you can do is specify clearly the limiter in your split :

s.split(' ')

Would give the following result: ['1', 'a', '3', 'bb', '', '', 'b8']

Now you have to interpret the '' as a ' ' needed to be added to the next not empty string. In the following for loop you will implement your "business rules" that put the white spaces in the expected place.

split_list = []
buffer = ''
for elt in temp_split:
    if elt != "":
        split_list.append(buffer   elt)
        buffer = ''
    else:
        buffer  = ' '
print(split_list)

And the result is: ['1', 'a', '3', 'bb', ' b8']

  • Related