user1795998
user1795998

Reputation: 5127

Not able to generate HTML code correctly

I am trying to read a list,values can be single or it can have multiple entries seperated by a comma,my goal is to append href link for the first value in the list ,col[0] namely,am running into following compilation error

INPUT:-
cols=['409452,  12345', '', '', 'This a test python script']

EXPECTED OUTPUT:-
 <tr>
<td><a href=http://data/409452>409452,<a href=http://data/12345>12345</a></td>
<td></td>
<td></td>
<td>This a test python script</td>

Python code:-

  cols=cols=['409452,  12345', '', '', 'This a test python script']

TEMPLATE = ['  <tr>']
for col in cols:
    value = col.split(",")  
    TEMPLATE.append(
        '    <td><a href=http://data/{}> {}</a></td>'.format(value)
TEMPLATE.append('  </tr>')
TEMPLATE = '\n'.join(TEMPLATE)
print TEMPLATE


Output I am getting:-


TEMPLATE.append('  </tr>')
       ^
SyntaxError: invalid syntax

Upvotes: 0

Views: 101

Answers (2)

nandhp
nandhp

Reputation: 4817

In addition to the missing ) mentioned by others, your use of format is incorrect (need to use *value to have it look for items in the array). (Your definition of cols also has the wrong indentation and has an extra cols=.)

This code works:

cols=['409452,  12345', '', '', 'This a test python script']

TEMPLATE = ['  <tr>']
for col in cols:
    if "," in col:
        value = col.split(",")
        value[:] = ['<a href=http://data/{0}>{0}</a>'.format(id.strip()) for id in value]
        col = ','.join(value)
    TEMPLATE.append('    <td>{}</td>'.format(col))
TEMPLATE.append('  </tr>')
TEMPLATE = '\n'.join(TEMPLATE)
print TEMPLATE

Output:

  <tr>
    <td><a href=http://data/409452>409452</a>,<a href=http://data/12345>12345</a></td>
    <td></td>
    <td></td>
    <td>This a test python script</td>
 </tr>

Upvotes: 2

larsks
larsks

Reputation: 312138

You haven't shown us your actual code (because there are not 13 lines in that sample, but your error message shows the error on line 13). However, in this case I think the answer is reasonably simple...take a close look at this line:

TEMPLATE.append(
    '    <td><a href=http://data/{}> {}</a></td>'.format(value)

Remove the string to make it more obvious:

TEMPLATE.append(''.format(value)

As you can see, you're missing a closing ).

Upvotes: 3

Related Questions