html脚本csv到html表

时间:2017-05-28 08:55:17

标签: python html python-2.7 csv

我对html一无所知。一些我如何获得将csv转换为html的代码。

以下是代码:

import sys
import csv
# Open the CSV file for reading


def populate_table(csv_fl):
    reader = csv.reader(open(csv_fl))
    # Create the HTML file for output

    html_table = ''
    # initialize rownum variable
    rownum = 0
    # write <table> tag
    html_table= '<table>\n'
    # generate table contents
    for row in reader: # Read a single row from the CSV file
        # write header row. assumes first row in csv contains header
        if rownum == 0:
            html_table += '<tr>\n' # write <tr> tag
            for column in row:
                html_table += '<th>' + column + '</th>\n'
            html_table += '</tr>\n'
        #write all other rows
        else:
            html_table += '<tr>\n'
            for column in row:
                if 'fail' in column or 'Fail' in column:
                    html_table += "<td style='color:red'>" + column + '</td>\n'

                    continue
                html_table += '<td>' + column + '</td>\n'

            html_table += '</tr>\n'
        #increment row count
        rownum += 1
    # write </table> tag
    html_table += '</table>\n'

    return html_table

如果字符串包含失败或失败,则上面的代码将生成红色单元格。

我需要这里的帮助才能使红线全线(非单细胞)。

下面是填写html的代码(缩进是错误的。如果需要正确的缩进代码,我将分享链接)。

我将在下面的代码下面执行:

python2.7 fil.py test.csv test.html 

import csv2html
import sys

class Sketch:
    def __init__(self):
        """
        Returns html sketch for a defined scenario
        Scenarios asccessible as functions.
        supported ones are:
       -fail
       -pass
       -status_update
       -final
       """

   def _style (self):
        body = """
        <style>
        p {
            font-family : Calibri;
            font-size: 14px;
            font-weight: bolder;
            text-align : left;
       }

       p.fade {
           color : #CCCCCC;
           font-size: 14px;
       }
        em  {
            font-style : italic ;
            font-size : 16px;
            font-weight: lighter ;
        }
        em.pass {
            font-style : italic ;
            font-size : 16px;
            color: green ;
        }
        em.fail {
           font-style : italic ;
           font-size : 16px;
           color: red ;
        }

        a {
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }

        hr {
            align: left ;
            margin-left: 0px ;
            width: 500px;
            height:1px;
        }

        table {
            border-collapse: collapse;
        }

        tr {
            padding: 4px;
            text-align: center;
            border-right:2px solid #FFFFFF;
        }

        tr:nth-child(even){background-color: #f2f2f2}

        th {
            background-color: #cceeff;
            color: black;
            padding: 4px;
            border-right:2px solid #FFFFFF;
        }


        </style>
        """
        return body

    def _start(self):
       return """
       <!DOCTYPE html>
       <html>
        """

    def _end(self):
       body ="""
       <hr/>
       <p class="fade">Note: Link might be disabled,
       please put me in safe sender list, by right 
       click on message.
       This is a system generated mail, please don't
       respond to it.</p>
       </html>
       """
       return body

        def _fail (self):
        body = """
       <p>STATUS :
           <em class="fail">failed</em>
       </p>
       """
       return body

   def _critical_fail(self):
       str_ = 'Failure is critical, terminating the run.'
       body = """
      <p>
          <em class="fail">%s</em>
       </p>
           """%str_
       return body 

   def _pass (self):
       body = """
       <p>STATUS :
           <em class="pass">passed</em>
       </p>
       """
       return body

   def _type (self, title, val):
        body = """
       <p>%s :
       <em>%s</em>
       </p>
       """%(title.upper(), val)
       return body

    def _loglink(self, logs):
       body = """ <p> LOGS :</p>
       <a href=%s>%s</a>
       """%(logs,logs)

       return body

   def render (self, test_id, descr, platform=None, pass_=True, \
               logs=None, critical=False):
       body = self._start() +\
              self._style() + \
              self._type("test id", test_id) + \
              self._type("description", descr) +\
              self._type("platform", platform)

       if pass_==True:
           body += self._pass ()
       else:
           body += self._fail ()
           if critical:
               body += self._critical_fail()

       body += self._loglink(logs)
       body += self._end()
       return body


   def status_update (self, ):
       pass

   def final (self, logs):
       body += self._end()
       return body

def add_html_header (csv_fl, fname):
   """ html data returned by sqlite needs to be enclosed in 
   some of the mandatory tags for the web to parse it 
   properly. ! """
   sketch =Sketch()
   content ="""
   %s %s 
       <body> 
           %s 
       </body> 
    </html>
    """%(sketch._start(), sketch._style(), csv2html.populate_table(csv_fl))
   open (fname, 'w').write (content)

 if len(sys.argv) < 3:
    print "Usage: csvToTable.py csv_file html_file"
    exit(1)

csv_fl = sys.argv[1]
html_fl = sys.argv[2]

add_html_header(csv_fl, html_fl)

1 个答案:

答案 0 :(得分:0)

简单地将整行用红色着色 src其中href是您想要着色的行。

&#13;
&#13;
<tr style="color:red">
&#13;
<tr>
&#13;
&#13;
&#13;