简体   繁体   中英

Split string into letters and numbers

I have a *string of letters and (negative)numbers and want to separate them into a array with python.

pathD = "M30,50.1c0,0,25,100,42,75s10.3-63.2,36.1-44.5s33.5,48.9,33.5,48.9l24.5-26.3"

splitS = re.split('(\d+)',pathD)

this does not work because it splits the dots and the dashes in one lined mess :

['M', '30', ',', '50', '.', '1', 'c', '0', ',', '0', ',', '25', ',', '100', ',', '42', ',', '75', 's', '10', '.', '3', '-', '63', '.', '2', ',', '36', '.', '1', '-', '44', '.', '5', 's', '33', '.', '5', ',', '48', '.', '9', ',', '33', '.', '5', ',', '48', '.', '9', 'l', '24', '.', '5', '-', '26', '.', '3', '']

i would like to see something like this:

 [M, 30, 50.1, c, 0, 0, 25, 100, 42, 75, s, 10.3, -63.2, 36.1, -44.5, s, 33.5, 48.9, 33.5, 48.9, l, 24.5, -26.3]

I am not sure if i am on the right path with this one or should approach it differently.

import re

pathD = "M30,50.1c0,0,25,100,42,75s10.3-63.2,36.1-44.5s33.5,48.9,33.5,48.9l24.5-26.3"

print(re.findall(r'[A-Za-z]|-?\d+\.\d+|\d+',pathD))

['M', '30', '50.1', 'c', '0', '0', '25', '100', '42', '75', 's', '10.3', '-63.2', '36.1', '-44.5', 's', '33.5', '48.9', '33.5', '48.9', 'l', '24.5', '-26.3']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM