首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成HTML代码时的逻辑问题

生成HTML代码时的逻辑问题
EN

Stack Overflow用户
提问于 2012-11-17 13:27:10
回答 1查看 115关注 0票数 0

我正在尝试使用下面的代码生成HTML代码(pastie链接),预期的输出如下所示,我得到的输出中CR值被添加了两次,并且http链接不是right...can任何人都可以帮助检查逻辑,看看我哪里出错了

我的代码:-

代码语言:javascript
复制
    http://pastie.org/5391102

INput在下面

代码语言:javascript
复制
http://pastie.org/5390316(if you copy/paste ,make sure they are tab seperated,otherwise you will get a key error)

预期输出:-

代码语言:javascript
复制
<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>

我当前的输出:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-18 10:40:12

这是我承诺的输入。

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
<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>

浏览器视图:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13427981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档