Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a lightweight language in Python
    primarykey
    data
    text
    <p>Say I define a string in Python like the following:</p> <pre><code>my_string = "something{name1, name2, opt1=2, opt2=text}, something_else{name3, opt1=58}" </code></pre> <p>I would like to parse that string in Python in a way that allows me to index the different structures of the language.</p> <p>For example, the output could be a dictionary <code>parsing_result</code> that allows me to index the different elements in a structred manner.</p> <p>For example, the following:</p> <pre><code>parsing_result['names'] </code></pre> <p>would hold a <code>list</code> of strings: <code>['name1', 'name2']</code></p> <p>whereas <code>parsing_result['options']</code> would hold a dictionary so that:</p> <ul> <li><code>parsing_result['something']['options']['opt2']</code> holds the string <code>"text"</code></li> <li><code>parsing_result['something_else']['options']['opt1']</code> holds the string <code>"58"</code></li> </ul> <p>My first question is: How do I approach this problem in Python? Are there any libraries that simplify this task? </p> <p>For a working example, I am not necessarily interested in a solution that parses the exact syntax I defined above (although that would be fantastic), but anything close to it would be great.</p> <h3>Update</h3> <ol> <li><p>It looks like the general right solution is using a parser and a lexer such as <a href="http://www.dabeaz.com/ply/" rel="nofollow noreferrer">ply</a> (thank you @Joran), but the documentation is a bit intimidating. Is there an easier way of getting this done when the syntax is <strong>lightweight</strong>?</p></li> <li><p>I found <a href="https://stackoverflow.com/questions/1648537/how-to-split-a-string-by-commas-positioned-outside-of-parenthesis">this thread</a> where the following regular expression is provided to partition a string around outer commas:</p> <pre><code>r = re.compile(r'(?:[^,(]|\([^)]*\))+') r.findall(s) </code></pre> <p>But this is assuming that the grouping character are <code>()</code> (and not <code>{}</code>). I am trying to adapt this, but it doesn't look easy.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload