-2

I need to parse the following text from an SVG.prop file:

parameters.styleList.0.fill=rgb (255, 0, 0) parameters.styleList.0.key=-2.74, -2.391666666666667 parameters.styleList.0.markerSize=10,10 parameters.styleList.0.stroke=rgb (255, 0, 0) parameters.styleList.0.strokeCapType= parameters.styleList.0.strokeDashArray= parameters.styleList.0.strokeJoinType= parameters.styleList.0.strokeWidth=0.5px parameters.styleList.0.symbol= parameters.styleList.0.title=-2.740 - -2.392 parameters.styleList.1.fill=rgb (255, 85, 0) parameters.styleList.1.key=-2.391666666666667, -2.0433333333333334 parameters.styleList.1.markerSize=10,10 parameters.styleList.1.stroke=rgb (255, 85, 0) parameters.styleList.1.strokeCapType= parameters.styleList.1.strokeDashArray= parameters.styleList.1.strokeJoinType= parameters.styleList.1.strokeWidth=0.5px parameters.styleList.1.symbol= parameters.styleList.1.title=-2.392 - -2.043 

I need to get, for each class (3rd value in lines "parameters.styleList.0.key") the "fill", "key" and "title" values. What is the best way of doing this in Python3? Thanks

2
  • What is your current approach? Did you try anything so far? Do you have had a look at other questions such as stackoverflow.com/questions/15857818/python-svg-parser ? Commented Jul 9, 2018 at 9:47
  • @dorvak That question is not relevant, as this is not an SVG file, but apparently a properties file describing an SVG somehow. Commented Jul 9, 2018 at 9:55

1 Answer 1

1

It's straightforward to parse with regexps, using the re module.

import re from collections import defaultdict from pprint import pprint data = """ parameters.styleList.0.fill=rgb (255, 0, 0) parameters.styleList.0.key=-2.74, -2.391666666666667 parameters.styleList.0.markerSize=10,10 parameters.styleList.0.stroke=rgb (255, 0, 0) parameters.styleList.0.strokeCapType= parameters.styleList.0.strokeDashArray= parameters.styleList.0.strokeJoinType= parameters.styleList.0.strokeWidth=0.5px parameters.styleList.0.symbol= parameters.styleList.0.title=-2.740 - -2.392 parameters.styleList.1.fill=rgb (255, 85, 0) parameters.styleList.1.key=-2.391666666666667, -2.0433333333333334 parameters.styleList.1.markerSize=10,10 parameters.styleList.1.stroke=rgb (255, 85, 0) parameters.styleList.1.strokeCapType= parameters.styleList.1.strokeDashArray= parameters.styleList.1.strokeJoinType= parameters.styleList.1.strokeWidth=0.5px parameters.styleList.1.symbol= parameters.styleList.1.title=-2.392 - -2.043 """ style_re = re.compile('^parameters\.styleList\.(?P<id>.+?)\.(?P<property>.+?)=(?P<value>.*)$', re.MULTILINE) styles = defaultdict(dict) for match in style_re.finditer(data): id, property, value = match.groups() styles[id][property] = value pprint(dict(styles)) 

outputs

{'0': {'fill': 'rgb (255, 0, 0)', 'key': '-2.74, -2.391666666666667', 'markerSize': '10,10', 'stroke': 'rgb (255, 0, 0)', 'strokeCapType': '', 'strokeDashArray': '', 'strokeJoinType': '', 'strokeWidth': '0.5px', 'symbol': '', 'title': '-2.740 - -2.392'}, '1': {'fill': 'rgb (255, 85, 0)', 'key': '-2.391666666666667, -2.0433333333333334', 'markerSize': '10,10', 'stroke': 'rgb (255, 85, 0)', 'strokeCapType': '', 'strokeDashArray': '', 'strokeJoinType': '', 'strokeWidth': '0.5px', 'symbol': '', 'title': '-2.392 - -2.043'}} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.