Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:39:14+00:00 2026-06-02T01:39:14+00:00

Have worked in dozens of languages but new to Python. My first (maybe second)

  • 0

Have worked in dozens of languages but new to Python.

My first (maybe second) question here, so be gentle…

Trying to efficiently convert HTML-like markdown text to wiki format (specifically, Linux Tomboy/GNote notes to Zim) and have gotten stuck on converting lists.

For a 2-level unordered list like this…

  • First level
    • Second level

Tomboy/GNote uses something like…

<list><list-item>First level<list><list-item>Second level</list-item></list></list-item></list>

However, the Zim personal wiki wants that to be…

* First level
  * Second level

… with leading tabs.

I’ve explored the regex module functions re.sub(), re.match(), re.search(), etc. and found the cool Python ability to code repeating text as…

 count * "text"

Thus, it looks like there should be a way to do something like…

 newnote = re.sub("<list>", LEVEL * "\t", oldnote)

Where LEVEL is the ordinal (occurrance) of <list> in the note. It would thus be 0 for the first <list> incountered, 1 for the second, etc.

LEVEL would then be decremented each time </list> was encountered.

<list-item> tags are converted to the asterisk for the bullet (preceded by newline as appropriate) and </list-item> tags dropped.

Finally… the question…

  • How do I get the value of LEVEL and use it as a tabs multiplier?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:39:15+00:00Added an answer on June 2, 2026 at 1:39 am

    You should really use an xml parser to do this, but to answer your question:

    import re
    
    def next_tag(s, tag):
        i = -1
        while True:
            try:
                i = s.index(tag, i+1)
            except ValueError:
                return
            yield i
    
    a = "<list><list-item>First level<list><list-item>Second level</list-item></list></list-item></list>"
    
    a = a.replace("<list-item>", "* ")
    
    for LEVEL, ind in enumerate(next_tag(a, "<list>")):
        a = re.sub("<list>", "\n" + LEVEL * "\t", a, 1)
    
    a = a.replace("</list-item>", "")
    a = a.replace("</list>", "")
    
    print a
    

    This will work for your example, and your example ONLY. Use an XML parser. You can use xml.dom.minidom (it’s included in Python (2.7 at least), no need to download anything):

    import xml.dom.minidom
    
    def parseList(el, lvl=0):
        txt = ""
        indent = "\t" * (lvl)
        for item in el.childNodes:
            # These are the <list-item>s: They can have text and nested <list> tag
            for subitem in item.childNodes:
                if subitem.nodeType is xml.dom.minidom.Element.TEXT_NODE:
                    # This is the text before the next <list> 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 = "<list><list-item>First level<list><list-item>Second level</list-item><list-item>Second level 2<list><list-item>Third level</list-item></list></list-item></list></list-item></list>"
    print parseXML(a)
    

    Output:

    * First level
        * Second level
        * Second level 2
            * Third level
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A question to maybe some who have worked extensively with WinUSB APIs or user
I have worked with pointers a lot, but still whenever I work with them,
I have worked many projects with no problems. But in current project, I tested
In the past I have worked with the microchip PIC family, but I would
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
I have worked through the answer provided here . I have been able to
I have worked in project for long time but after electricity has cut off
I have worked out how to detect touching on an object using glReadPixels but
I have worked within a web development company where we had our local machines,
I have worked with SOAP in SAAJ and JAXM, and I want to extend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.