0

I am trying generate HTML code using the code below(pastie link),expected output is shown below ,am getting an output where the CR value is getting added twice and also the http links are not right...can anyone help review the logic to see where I am going wrong

My code:-

    http://pastie.org/5391102

INput is below

http://pastie.org/5390316(if you copy/paste ,make sure they are tab seperated,otherwise you will get a key error)

Expected output:-

<table cellspacing="1" cellpadding="1" border="1">
<tr>
<th bgcolor="#67B0F9" scope="col">CR</th>
<th bgcolor="#67B0F9" scope="col">FA</th>
 <th bgcolor="#67B0F9" scope="col">CL</th>
<th bgcolor="#67B0F9" scope="col">Title</th>
 </tr>
<tr>
 <td><a href="http://prism/CR/409452">409452</a></td>
 <td>WLAN</td>
<td>656885</td>
<td>Age out RSSI values from buffer </td>
</tr>
<tr>
<td><a href=http://data/409452>409452</a>,<a href=http://data/12345>12345</a></td>
<td></td>
<td></td>
<td>To Record HAL and SLM FW Event Logging</td>
</tr>
</table>

My current output:

<table cellspacing="1" cellpadding="1" border="1">
<tr>
<th bgcolor="#67B0F9" scope="col">CR</th>
<th bgcolor="#67B0F9" scope="col">FA</th>
<th bgcolor="#67B0F9" scope="col">CL</th>
<th bgcolor="#67B0F9" scope="col">Title</th>
</tr>
 <tr>
  <td><a href="http://prism/CR/409452">409452</a></td>
<td><a href="http://prism/CR/409452">409452</a></td>
 <td><a href=http://prism/CR/Title>Title</a></td>
 <td>wlan</td>
 <td>656885</td>
 <td>Age out rssi values from buffer</td>
</tr>
 <tr>
<td><a href="http://prism/CR/409452, 12345">409452, 12345</a></td>
<td><a href="http://prism/CR/409452, 12345">409452, 12345</a></td>
<td><a href=http://prism/CR/Title>Title</a></td>
<td></td>
<td></td>
 <td>To Record HAL and SLM FW Event Logging</td>
 </tr>
</table>
3
  • Your posted code has errors - you should post the fixed code that results in this output. Commented Nov 17, 2012 at 6:56
  • @thomas - i EDITED..here it is pastie.org/5391102 Commented Nov 17, 2012 at 7:37
  • @martin - do you have any inputs on this pleasE? Commented Nov 18, 2012 at 0:21

1 Answer 1

1

Here's that input I promised.

def CRlistToTable(CRlist):
    """ Create HTML table from CRlist data """
    CRstrings = ['<table cellspacing="1" cellpadding="1" border="1">']

    # create table's header row from the first row of CRlist
    BGCOLOR = '#67B0F9' # column header cells background color
    cols = CRlist[0]  # column names given in first row
    CRstrings += ['  <tr>', '\n'.join(
                  '    <th bgcolor="{}" scope="col">{}</th>'.format(BGCOLOR, col_name)
                     for col_name in cols),
                  '  </tr>']

    # create a template for remaining non-header table rows
    TR_TEMPLATE = ['  <tr>',
                   '    <td>',
                   '      {}',  # for dynamically generated CR links
                   '    </td>', '\n'.join(
                  ['    <td>{}</td>'] * (len(cols)-1)), # one per remaining cols
                   '  </tr>']
    TR_TEMPLATE = '\n'.join(TR_TEMPLATE) # convert to a string

    # apply the row template created to remaining CRlist rows
    CR_LINK_TEMPLATE = '<a href=http://data/{0}>{0}</a>'
    for row in CRlist[1:]:
        if ',' not in row[0]:  # no comma-delimited items in first column?
            links = CR_LINK_TEMPLATE.format(row[0])
        else:
            CRs = row[0].replace(',', ' ').split()
            links = ',\n      '.join(CR_LINK_TEMPLATE.format(cr) for cr in CRs)
        row[0] = links
        CRstrings += [TR_TEMPLATE.format(*row)]

    CRstrings += ["</table>"]

    # return string list merged to a single long newline-delimited string
    return '\n'.join(CRstrings) + '\n'


with open('cr_fixes_tabbed.xml') as file:
    xmldata = file.read()  # read entire file into memory

FIXES_START_TAG, FIXES_END_TAG = '<Fixes>, </Fixes>'.replace(',', ' ').split()
# embedded fixes info starts right after the tag itself within the xml data
xmlFixesStart = xmldata.find(FIXES_START_TAG) + len(FIXES_START_TAG)
xmlFixesEnd = xmldata.find(FIXES_END_TAG)

# extract portion of file data within the FIXES tags into a list of lines
info = xmldata[xmlFixesStart:xmlFixesEnd].strip().splitlines()

# split non-blank lines of tab-delimited data into list of rows of column data
CRlist = [line.split('\t') for line in info if line] # skips blank lines

crInfo = CRlistToTable(CRlist) # convert list into html table
print crInfo

Output:

<table cellspacing="1" cellpadding="1" border="1">
  <tr>
    <th bgcolor="#67B0F9" scope="col">CR</th>
    <th bgcolor="#67B0F9" scope="col">FA</th>
    <th bgcolor="#67B0F9" scope="col">CL</th>
    <th bgcolor="#67B0F9" scope="col">Title</th>
  </tr>
  <tr>
    <td>
      <a href=http://data/409452>409452</a>
    </td>
    <td>WLAN</td>
    <td>656885</td>
    <td>Age out RSSI values from buffer</td>
  </tr>
  <tr>
    <td>
      <a href=http://data/409452>409452</a>,
      <a href=http://data/12345>12345</a>
    </td>
    <td></td>
    <td></td>
    <td>To Record HAL and SLM FW Event Logging</td>
  </tr>
</table>

Browser view:

html output viewed in browser

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

4 Comments

Thanks,one problem i am seeing if the columns are more,for input like pastie.org/5395174 its giving an error like CRstrings += [TR_TEMPLATE.format(*row)] IndexError: tuple index out of range
Thanks a lot for your support..you have been excellent support,dont know what should i do to repay your support :-)
one question,what does {0} in CR_LINK_TEMPLATE = '<a href=data{0}>{0}</a>' do?,i mean why {0},why not {1}?
The syntax for replacement fields in format strings optionally allows an element index between the brackets. Normally I don't bother, however it is useful in situations where there's a need to reference the same argument more than once in the string. i.e. '{0}, {0}'.format(x) vs '{}, {}'.format(x, x)

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.