0

我正在使用以下代码创建一个表该表将包含四列..请建议需要对代码进行哪些更改才能实现这一点

输入 XML:-

<Fixes>
CR           FA      CL                    TITLE

409452      WLAN    656885        Age out RSSI values from buffer in Beacon miss scenario  
12345,45678  BT     54567,34567   Test
379104       BT     656928        CR379104: BT doesn’t work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.
</Fixes>

Python代码

crInfo = [ ]
CRlist = [ ]
CRsFixedStart=xmlfile.find('<Fixes>')
CRsFixedEnd=xmlfile.find('</Fixes>')
info=xmlfile[CRsFixedStart+12:CRsFixedEnd].strip()
for i in info.splitlines():
    index = i.split(None, 3)
    CRlist.append(index)
crInfo= CRlisttable(CRlist)
file.close()

def CRlisttable(CRlist,CRcount):
#For logging
global logString
print "\nBuilding the CRtable\n"
logString += "Building the build combo table\n"
#print "CRlist"
#print CRlist
CRstring = "<table cellspacing=\"1\" cellpadding=\"1\" border=\"1\">\n"
CRstring += "<tr>\n"
CRstring += "<th bgcolor=\"#67B0F9\" scope=\"col\">" + CRlist[0][0] + "</th>\n"
CRstring += "<th bgcolor=\"#67B0F9\" scope=\"col\">" + CRlist[0][1] + "</th>\n"
CRstring += "<th bgcolor=\"#67B0F9\" scope=\"col\">" + CRlist[0][2] + "</th>\n"
CRstring += "<th bgcolor=\"#67B0F9\" scope=\"col\">" + CRlist[0][3] + "</th>\n"
CRstring += "</tr>\n"

TEMPLATE = """
<tr>
<td><a href='http://prism/CR/{CR}'>{CR}</a></td>
<td>{FA}</td>
<td>{CL}</td>
<td>{Title}</td>
</tr>
"""
for item in CRlist[1:]:
    CRstring += TEMPLATE.format(
        CR=item[0],
        FA=item[1],
        CL=item[2],
        Title=item[3],
        )
CRstring += "\n</table>\n"
#print CRstring
return CRstring
4

2 回答 2

3

虽然我对提供这个有一些保留,因为你似乎不愿意自己尝试这样做,但这里有一个例子展示了它可以做到的一种方式——所有这些都是希望至少你会倾向于努力学习和即使它被交给你,也可能从中学到一些东西......

with open('cr_fixes.xml') as file: # get some data to process
    xmlfile = file.read()

def CRlistToTable(CRlist):
    cols = CRlist[0] # first item is header-row of col names on the first line

    CRstrings = ['<table cellspacing="1" cellpadding="1" border="1">']
    # table header row
    CRstrings.append('  <tr>')
    for col in cols:
        CRstrings.append('    <th bgcolor="#67B0F9" scope="col">{}</th>'.format(col))
    CRstrings.append('  </tr>')

    # create a template for each table row
    TR_TEMPLATE = ['  <tr>']
    # 1st col of each row is CR and handled separately since it corresponds to a link
    TR_TEMPLATE.append(
        '    <td><a href="http://prism/CR/{{{}}}">{{{}}}</a></td>'.format(*[cols[0]]*2))
    for col in cols[1:]:
        TR_TEMPLATE.append('    <td>{{}}</td>'.format(col))
    TR_TEMPLATE.append('  </tr>')
    TR_TEMPLATE = '\n'.join(TR_TEMPLATE)

    # then apply the template to all the non-header rows of CRlist
    for items in CRlist[1:]:
        CRstrings.append(TR_TEMPLATE.format(CR=items[0], *items[1:]))
    CRstrings.append("</table>")

    return '\n'.join(CRstrings) + '\n'

FIXES_START_TAG, FIXES_END_TAG = '<Fixes>, </Fixes>'.replace(',', ' ').split()
CRsFixesStart = xmlfile.find(FIXES_START_TAG) + len(FIXES_START_TAG)
CRsFixesEnd = xmlfile.find(FIXES_END_TAG)
info = xmlfile[CRsFixesStart:CRsFixesEnd].strip().splitlines()

# first line of extracted info is a blank-separated list of column names
num_cols = len(info[0].split())

# split non-blank lines of info into list of columnar data
# assuming last col is the variable-length title, comprising reminder of line
CRlist = [line.split(None, num_cols-1) for line in info if line]

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

输出:

<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 in Beacon miss scenario</td>
  </tr>
  <tr>
    <td><a href="http://prism/CR/12345,45678">12345,45678</a></td>
    <td>BT</td>
    <td>54567,34567</td>
    <td>Test</td>
  </tr>
  <tr>
    <td><a href="http://prism/CR/379104">379104</a></td>
    <td>BT</td>
    <td>656928</td>
    <td>CR379104: BT doesnt work that Riva neither sends HCI Evt for HID ACL data nor 
        response to HCI_INQUIRY after entering into pseudo sniff subrating mode.</td>
  </tr>
</table>

在此处输入图像描述

于 2012-11-11T23:28:25.333 回答
0

这看起来不像 XML 文件 - 它看起来像一对标签内的制表符分隔的 CSV 文档。

我建议查看csv用于解析输入文件的模块,然后jinja2查看用于编写 HTML 生成的模板引擎。

本质上 - 读取 csv,检查标题的长度(为您提供列数),然后将该数据传递到模板中。在模板中,您将在 csv 结构上循环以生成 HTML。

于 2012-11-11T07:55:39.697 回答