2

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.

Blikpils
  • 27
  • 1
  • 5
  • 2
    If thats SVG path, there are SVG tools for python that I would use for this... – reptilicus Nov 04 '15 at 22:38
  • 1
    `pathD` is a string then (so the value should be surrounded by quotes to be valid python syntax)? you say it is a list, but perform a regex on it like it is a string. – krock Nov 04 '15 at 22:40

1 Answers1

3
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']
LetzerWille
  • 5,355
  • 4
  • 23
  • 26