0

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
user1795998
  • 4,937
  • 7
  • 23
  • 23

2 Answers2

3

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 ).

larsks
  • 277,717
  • 41
  • 399
  • 399
2

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>
Community
  • 1
  • 1
nandhp
  • 4,680
  • 2
  • 30
  • 42
  • if i try the above code,am getting the following error TEMPLATE.append(' {}'.format(*value)) IndexError: tuple index out of range – user1795998 Nov 15 '12 at 04:03
  • It sounds like `value` contains fewer than two elements. Are you using the value of `cols` from my code? Try putting a `print value` before that line to see what's wrong. – nandhp Nov 15 '12 at 04:06
  • ya..it doesn contain fewer than two elements sometimes,input should be cols=cols=['409452, 12345', '', '', 'This a test python script'],as you can see only the first value has two elements,sometimes there is no value and – user1795998 Nov 15 '12 at 04:14
  • `value` needs to have two elements, since you have two occurrences of `{}` in the string. You'll need to do something after the split to make sure `value` has enough elements, or maybe you'll need to do something else if `value` has only one item, or maybe you want to use `{0}` instead of `{}` (so that you get the first item of `value` in both places). You need to figure out what you want the code to do in that case. – nandhp Nov 15 '12 at 04:19
  • you missed the whole point in my question,anyways thanks for trying – user1795998 Nov 15 '12 at 04:30
  • Sorry, I missed your "expected output" the first time around. I've updated my answer and I believe I have come closer to answering your question now, unless you also have different behavior in mind for the line with the comma. Is that supposed to be a separate link for each item separated by commas? – nandhp Nov 15 '12 at 04:38
  • no probs..but it's still not my expected output..I will see what I can do unless you have any better idea – user1795998 Nov 15 '12 at 04:45
  • I've edited it again, and I think this is now what you're looking for. It doesn't match your output exactly -- your example output seems to be missing a `` for the first item, which my version does output -- but that's easy for you to change if you need to be exactly the same (Get rid of `` from where it is and put `+''` at end of next line). Sorry for not *reading* your question carefully enough before trying to answer it. – nandhp Nov 15 '12 at 04:55
  • 1
    @user1795998: FWIW, I think this answer is on the right track. – martineau Nov 15 '12 at 09:19
  • 1
    You don't need to write `value[:] = [' – martineau Nov 15 '12 at 11:42