I'm building an XML parser in python for an SVG file. It will eventually become specific instructions for stepper motors.
SVG files contain commands such as 'M', 'C' and 'L.' The path data might look like this:
[M199.66, 0.50C199.6, 0.50...0.50Z]
When I extracted the path data, it's a list of one item (which is a string). I split the long string into multiple strings:
[u'M199.6', u'0.50C199.66', u'0.50']
The 'M, C and L' commands are important - I'm having difficulty splitting '0.5C199.6' into '0.5' and 'C199.6' because it only exists for certain items in the list, and I'd like to retain the C and not discard it. This is what I have so far:
for item in path_strings[0]:
s=string.split(path_strings[0], ',')
print s
break
for i in range(len(s)):
coordinates=string.split(s[i],'C')
print coordinates
break
You could try breaking it into substrings like this:
whole = "0.5C199.66"
start = whole[0:whole.find("C")]
end = whole[whole.find("C"):]
That should give you start == "0.5"
and end == "C199.66"
Alternatively you could use the index function instead of find, which raises a ValueError when the substring can't be found. That would give you the benefit of easily determining that for the current string, no 'C' command is present.
http://docs.python.org/2/library/string.html#string-functions
Use a regex to search for the commands ( [MCL]
).
import re
lst = [u'M199.6', u'0.50C199.66', u'0.50']
for i, j in enumerate(lst):
m = re.search('(.+?)([MCL].+)', j)
if m:
print [m.group(1), m.group(2)] # = coordinates from your example
lst[i:i+1] = [m.group(1), m.group(2)] # replace the item in the lst with the splitted thing
# or do something else with the coordinates, whatever you want.
print lst
splits your list in:
[u'M199.6', u'0.50', u'C199.66', u'0.50']
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.