生成HTML代码的逻辑问题 [英] Logical issue in generating HTML code

查看:99
本文介绍了生成HTML代码的逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用下面的代码生成HTML代码(pastie链接),期望的输出如下所示,得到一个输出,其中CR值被添加两次,并且http链接也不正确......任何人都可以帮助审查逻辑,看看我去哪里错了我的代码:

pre $ $ c
$ b $ $ c> http://pastie.org/5391102

输入低于

  http://pastie.org/5390316(如果您复制/粘贴,请确保它们是制表符分隔的,否则您将得到一个关键错误)

预期输出: -

 < table cellspacing =1cellpadding =1border =1> 
< tr>
< th bgcolor =#67B0F9scope =col> CR< / th>
< th bgcolor =#67B0F9scope =col> FA< / th>
< th bgcolor =#67B0F9scope =col> CL< / th>
< th bgcolor =#67B0F9scope =col>标题< / th>
< / tr>
< tr>
< td>< a href =http:// prism / CR / 409452> 409452< / a>< / td>
< td> WLAN< / td>
< td> 656885< / td>
< td>将缓冲区中的RSSI值放大< / td>
< / tr>
< tr>
< td>< a href = http:// data / 409452> 409452< / a>,< a href = http:// data / 12345> 12345< / a>< / td>
< td>< / td>
< td>< / td>
< td>记录HAL和SLM固件事件记录< / td>
< / tr>
< / table>

我目前的输出:

 < table cellspacing =1cellpadding =1border =1> 
< tr>
< th bgcolor =#67B0F9scope =col> CR< / th>
< th bgcolor =#67B0F9scope =col> FA< / th>
< th bgcolor =#67B0F9scope =col> CL< / th>
< th bgcolor =#67B0F9scope =col>标题< / 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>标题< / a>< / td>
< td> wlan< / td>
< td> 656885< / td>
< td>超出缓冲区的rssi值< / 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>标题< / a>< / td>
< td>< / td>
< td>< / td>
< td>记录HAL和SLM固件事件记录< / td>
< / tr>
< / table>


解决方案

这是我承诺的输入。 b
$ b

  def CRlistToTable(CRlist):
从CRlist数据创建HTML表格
CRstrings = ['< ; table cellspacing =1cellpadding =1border =1>']

#从CRlist的第一行创建表格的标题行
BGCOLOR ='#67B0F9' #列标题单元格背景颜色
cols = CRlist [0]#列名在第一行给出
CRstrings + = ['< tr>','\\\
'.join(
'< th bgcolor ={}scope =col> {}< / th>'格式(BGCOLOR,col_name)
for col_name in cols),
'< tr>']

#为其余的非表头行创建模板
TR_TEMPLATE = ['< tr>',
'< td>',
'{}',#用于动态生成CR链接s
'< / td>','\ n'.join(
['td> {}< / td>'] *(len(cols)-1)) ,#每个剩余列数
'< / tr>']
TR_TEMPLATE ='\\\
'.join(TR_TEMPLATE)#转换为字符串

#apply the为其余CRlist行创建的行模板
CR_LINK_TEMPLATE ='< a href = http:// data / {0}> {0}< / a>'
用于CRlist中的行[1: ]:
if','not in row [0]:#第一列没有逗号分隔的项目?
links = CR_LINK_TEMPLATE.format(row [0])
else:
CRs = row [0] .replace(',','').split()
链接=',\ n'.join(CR中CR的CR_LINK_TEMPLATE.format(cr))
row [0] = links
CRstrings + = [TR_TEMPLATE.format(* row)]

CRstrings + = [< / table>]]

#返回字符串列表合并为一个长整型换行符字符串
return'\\\
'.join (CRstrings)+'\ n'


打开('cr_fixes_tabbed.xml')作为文件:
xmldata = file.read()#将整个文件读入内存

FIXES_START_TAG,FIXES_END_TAG ='< Fixes>,< / Fixes>'。replace(',','').split()
#在xml数据中标记自身
xmlFixesStart = xmldata.find(FIXES_START_TAG)+ len(FIXES_START_TAG)
xmlFixesEnd = xmldata.find(FIXES_END_TAG)

#提取部分文件数据FIXES标记到行列表中
info = xmldata [xmlFixesStart:xmlFixesEnd] .strip()。splitlines()

将制表符分隔数据的非空行分割成列数据的行
CRlist = [line.split('\t')for line in info if line]#跳过空行

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

输出:

 < table cellspacing =1cellpadding =1border =1> 
< tr>
< th bgcolor =#67B0F9scope =col> CR< / th>
< th bgcolor =#67B0F9scope =col> FA< / th>
< th bgcolor =#67B0F9scope =col> CL< / th>
< th bgcolor =#67B0F9scope =col>标题< / th>
< / tr>
< tr>
< td>
< a href = http:// data / 409452> 409452< / a>
< / td>
< td> WLAN< / td>
< td> 656885< / td>
< td>将缓冲区中的RSSI值放大< / td>
< / tr>
< tr>
< td>
< a href = http:// data / 409452> 409452< / a>,
< a href = http:// data / 12345> 12345< / a>
< / td>
< td>< / td>
< td>< / td>
< td>记录HAL和SLM固件事件记录< / td>
< / tr>
< / table>

浏览器视图:


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>

解决方案

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代码的逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆