0

I am trying to construct a HTML table in python and running into the following syntax error,can anyone please point what is the issue here?

for i in range(len(PLlis)):
    print "i"
    print i
    outstring += "<tr>\n"
    outstring += "<td><a href="wikilinklis[i]">PLlist[0]</a></td>\n"
    outstring += "<td>build_locationlis[i]</td>\n"
    outstring += "<td>lis[i]</td>\n"
    outstring += "<td>Host_loc</td>\n"
    outstring += "<td>Baselis[i]</td>\n"
    outstring += "</tr>\n"
outstring += "</table>\n"
return outstring

SYNTAX ERROR:-

   outstring += "<td><a href="wikilinklis[i]">PLlist[0]</a></td>\n"
                                     ^

SyntaxError: invalid syntax

1
  • Please don't change your question this radically. This makes all past answers look unfitting, while at the same time the question already hase recieved enough attention so it may be ignored. It's better to create a new question. Commented Nov 4, 2012 at 22:03

4 Answers 4

4

concatenate your strings:

outstring += "<td><a href=" + wikilinklis[i] + ">PLlist[0]</a></td>\n"

if wikilinks is a python array of strings. Otherwise you have to escape the quotes (if you're trying to write 'wikilinks[i]' as a string).

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

Comments

2

You have to rebuild your string like this, because wikilinklis[i] changes for every iteration.

outstring += "<td><a href=%s>%s</a></td>\n" % (wikilinklis[i], PLlist[0])

Comments

0

Don't do what you're doing. Instead of concatenating a bunch of strings, you want to substitute values into a template.

The simplest way to do that is with a string, and the % operator:

"""
<tr>
<td><a href="%(wikilinklis)s">%(PLlist)s</a></td>
<td>%(build_locationlis)s</td>
</tr>
""" % {'wikilinks': 'http://foo', 'PLlist': 'A link' }

Note the triple-quotes, which allow you to embed newlines and quotes.

Comments

0

Python does not have built in string interpolation. However, you can easily get what you want with "formatstring".format(...)

for i in range(len(PLlis)):
    print "i"
    print i
    outstring += """
    <tr>
        <td><a href="{wikilink}">{PLlist[0]}</a></td>
        <td>{build_location}</td>
        <td>{value}</td>
        <td>Host_loc</td>
        <td>{base}</td>
    </tr>""".format(
        wikilink=wikilinklis[i],
        build_location=build_locationlis[i],
        value=lis[i],
        base=Baselis[i]
    )

outstring += "</table>\n"
return outstring

The triple quote have no meaning other than to allow me to span a string over multiple lines.

7 Comments

Python does have string interpolation - check out the % operator.
@Eric,running into following error,does this work with python 2.7.3? </tr>""".format(**locals()) TypeError: list indices must be integers, not str
@Marcin: I was trying to describe "automatic" string interpolation
@Eric ,one list which looks like [None, '\\\\skinnycow\\builds498\\INTEGRATION\\F8064AAAAANLGD1620W.2\r'] gets constructed as "</td> <td>\\skinnycow\builds498\INTEGRATION\F8064AAAAANLGD1620W.2",as you can see "\td" tag is at the front..is it because of carriage return?
@user1795998: Yes. If you print out a \r to the console, the line will jump back.
|

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.