Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should really use an xml parser to do this, but to answer your question:</p> <pre><code>import re def next_tag(s, tag): i = -1 while True: try: i = s.index(tag, i+1) except ValueError: return yield i a = "&lt;list&gt;&lt;list-item&gt;First level&lt;list&gt;&lt;list-item&gt;Second level&lt;/list-item&gt;&lt;/list&gt;&lt;/list-item&gt;&lt;/list&gt;" a = a.replace("&lt;list-item&gt;", "* ") for LEVEL, ind in enumerate(next_tag(a, "&lt;list&gt;")): a = re.sub("&lt;list&gt;", "\n" + LEVEL * "\t", a, 1) a = a.replace("&lt;/list-item&gt;", "") a = a.replace("&lt;/list&gt;", "") print a </code></pre> <p>This will work for your example, and <strong>your example ONLY</strong>. Use an XML parser. You can use <code>xml.dom.minidom</code> (it's included in Python (2.7 at least), no need to download anything):</p> <pre><code>import xml.dom.minidom def parseList(el, lvl=0): txt = "" indent = "\t" * (lvl) for item in el.childNodes: # These are the &lt;list-item&gt;s: They can have text and nested &lt;list&gt; tag for subitem in item.childNodes: if subitem.nodeType is xml.dom.minidom.Element.TEXT_NODE: # This is the text before the next &lt;list&gt; tag txt += "\n" + indent + "* " + subitem.nodeValue else: # This is the next list tag, its indent level is incremented txt += parseList(subitem, lvl=lvl+1) return txt def parseXML(s): doc = xml.dom.minidom.parseString(s) return parseList(doc.firstChild) a = "&lt;list&gt;&lt;list-item&gt;First level&lt;list&gt;&lt;list-item&gt;Second level&lt;/list-item&gt;&lt;list-item&gt;Second level 2&lt;list&gt;&lt;list-item&gt;Third level&lt;/list-item&gt;&lt;/list&gt;&lt;/list-item&gt;&lt;/list&gt;&lt;/list-item&gt;&lt;/list&gt;" print parseXML(a) </code></pre> <p>Output:</p> <pre><code>* First level * Second level * Second level 2 * Third level </code></pre>
 

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