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

  • Home
  • SEARCH
  • 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 8489855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:56:37+00:00 2026-06-10T21:56:37+00:00

I’m a beginner programmer so this question might sound trivial: I have some text

  • 0

I’m a beginner programmer so this question might sound trivial: I have some text files containg tab-delimited text like:

A
    B
    C
        D
        E

Now I want to generate unordered .html lists out of this, with the structure:

<ul>
<li>A
<ul><li>B</li>
<li>C
<ul><li>D</li>
<li>E</li></ul></li></ul></li>
</ul>

My idea was to write a Python script, but if there is an easier (automatic) way, that is fine too. For identifying the indentation level and item name I would try to use this code:

import sys
indent = 0
last = []
for line in sys.stdin:
    count = 0
    while line.startswith("\t"):
       count += 1
       line = line[1:]
    if count > indent:
       indent += 1
       last.append(last[-1])
    elif count < indent:
       indent -= 1
       last = last[:-1]
  • 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-10T21:56:39+00:00Added an answer on June 10, 2026 at 9:56 pm

    tokenize module understands your input format: lines contain a valid Python identifiers, the indentation level of the statements is significant. ElementTree module allows you to manipulate tree structures in memory so it might be more flexable to separate a tree creation from a rendering it as html:

    from tokenize import NAME, INDENT, DEDENT, ENDMARKER, NEWLINE, generate_tokens
    from xml.etree import ElementTree as etree
    
    def parse(file, TreeBuilder=etree.TreeBuilder):
        tb = TreeBuilder()
        tb.start('ul', {})
        for type_, text, start, end, line in generate_tokens(file.readline):
            if type_ == NAME: # convert name to <li> item
                tb.start('li', {})
                tb.data(text)
                tb.end('li')
            elif type_ == NEWLINE:
                continue
            elif type_ == INDENT: # start <ul>
                tb.start('ul', {})
            elif type_ == DEDENT: # end </ul>
                tb.end('ul')
            elif type_ == ENDMARKER: # done
                tb.end('ul') # end parent list
                break
            else: # unexpected token
                assert 0, (type_, text, start, end, line)
        return tb.close() # return root element
    

    Any class that provides .start(), .end(), .data(), .close() methods can be used as a TreeBuilder e.g., you could just write html on the fly instead of building a tree.

    To parse stdin and write html to stdout you could use ElementTree.write():

    import sys
    
    etree.ElementTree(parse(sys.stdin)).write(sys.stdout, method='html')
    

    Output:

    <ul><li>A</li><ul><li>B</li><li>C</li><ul><li>D</li><li>E</li></ul></ul></ul>
    

    You can use any file, not just sys.stdin/sys.stdout.

    Note: To write to stdout on Python 3 use sys.stdout.buffer or encoding="unicode" due to bytes/Unicode distinction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.