There are multiple spaces in a string, how to divide them only according to the first space

has a string like this

"000001 "
"000002   "

I want to divide it into the form of stock symbol + company name by regular
this is my regular

re.split("\\s+", string)

but the segmented result is

"000001",""
"000002","",""

how can it be divided into

"000002"," "
The form of

, that is, the space segments that are divided according to the first space and the space segments that appear again do not match the regularity.


if it's just standardized data for stocks like the above (six digits that I know should be standard), you don't have to cut by spaces, you can cut the first six characters, and then tirm the spaces around the string.

Of course, you can also cut

by space. You can use re.split ("\\ s +", string, 1)

.

reference:

re.split(pattern, string, maxsplit=0)
The

maxsplit parameter indicates the number of splits. If it is set to 1 here, it will only be sliced once, and the following spaces will not be cut.


-sharpcoding=utf-8
import re
s = '0010000  A'
print re.split('(?<=\d) ', s)

simplest:

'000001 '.replace(' ', ',')
// "000001,"
Menu