0

Like for example I have a string of HTML Ordered List. Inside that ordered list, I want to write n number of lists. How can I accomplish the task of adding the lists to this string?

Here is the example code:

    html = """
    <ol>
      <li>
        <!--Something-->
      </li>
      ... <!--n lists-->
      {}                   #str().format()
      <li>
        <!--Something-->
      </li>
    </ol>
    """
    for li in html_lists: #where li is <li>...</li> and is inside the python list.
      html.format(li)

As far as I know, Strings are immutable and .format() will add <li> at {}. Hence this won't work for more than one <li>.

2
  • html.format('\n'.join(html_lists))? Commented Aug 18, 2013 at 15:53
  • .format() will not add <li> at {} it will create a new string that has li in the place of {} Commented Aug 18, 2013 at 15:55

3 Answers 3

2

Like you said, strings are immutable so just having html.format(li) on a line by itself won't do anything, you need to do html = html.format(li) because the first version won't modify html in place, it will return a result.

As for using a loop with str.format(), you should be able to use the following assuming each element in html_lists is a string that contains a single <li> entry:

html = html.format('\n'.join(html_lists))

This works because '\n'.join(html_lists) will construct a single string from your list of strings, which can then be passed to html.format() to replace the single {} with the content from every element in html_lists. Note that you could also use ''.join(html_lists), the newline is just there to make it more readable when html is displayed.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use lxml to build HTML:

import lxml.html as LH
import lxml.builder as builder
html_lists = 'one two three'.split()
E = builder.E
html = (
    E.ol(
        *([E.li('something')]
          + [E.li(item) for item in html_lists]
          + [E.li('else')])
        )
    )

print(LH.tostring(html, pretty_print=True))

prints

<ol>
<li>something</li>
<li>one</li>
<li>two</li>
<li>three</li>
<li>else</li>
</ol>

Comments

0

Python is really good for processing text, so here's an example using it to do what you want:

import textwrap

def indent(amt, s):
    dent = amt * ' '
    return ''.join(map(lambda i: dent+i+'\n', s.split('\n')[:-1])).rstrip()

ordered_list_html = textwrap.dedent('''\
    <ol>
    {}
    </ol>
''')

# create some test data
html_lists = [textwrap.dedent('''\
    <li>
      list #{}
    </li>
    ''').format(n) for n in xrange(5)]

print ordered_list_html.format(indent(2, ''.join(html_lists)))

Output:

<ol>
  <li>
    list #0
  </li>
  <li>
    list #1
  </li>
  <li>
    list #2
  </li>
  <li>
    list #3
  </li>
  <li>
    list #4
  </li>
</ol>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.