share
Stack OverflowHandle space as a separator
[-3] [1] Awais Khan
[2015-12-16 18:26:53]
[ python python-3.x ]
[ https://stackoverflow.com/questions/34319455/handle-space-as-a-separator ]

I have a string:

s = '1234     Q-24 2010-11-29         563 abc  a6G47er15                        '

and I want to convert it into an Array with spaces.

['1234', ' ', 'Q-24', '2010-11-29', ' ', ' ', '563', 'abc', 'a6G47er15', ' ', ' ']

Please help me I am new with python

(8) How are the number of ' ' strings in the output determined? - Martijn Pieters
(4) And what, if anything, have you tried yourself yet? - Martijn Pieters
Sorry semicolon is just typo mistake. spaces range is not defined and I have used split() and some custom solution which was suggested here, but no was fulfilling my requirements. - Awais Khan
Mea Culpa, it appears it was me that introduced the semi-colon. Corrected. - Martijn Pieters
spaces range is not defined: nobody can help you if you don't know exactly what you want... - stellasia
Some people will upvote anything... - Two-Bit Alchemist
[0] [2015-12-16 23:29:01] TextGeek

This may be enough:

parts = re.split(r'( )', s)

The regex split() function returns an array made by breaking the second argument at each match to the first argument. Normally, the matches themselves (in this case, each space) are discarded. But with the parentheses (called a "capture" in regex-speak), they are kept as separate items in the array as well.

-s


(4) Guesses on an unclear question may or may not be useful to the OP, but (especially with no explanation) they are of no value to anyone else. - Nathan Tuggy
1