2017-05-28 77 views
0

我對html沒有任何的想法。一些如何獲得代碼將csv轉換爲html。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(縮進是錯誤的,如果需要正確的縮進代碼,我會分享鏈接)。

我會EXCUTE下面的代碼象下面這樣:

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) 

回答

0

顏色全行紅,只是 <tr style="color:red">其中<tr>是你想要的顏色的行。

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; 
 
}
<table> 
 
    <tr> 
 
    <th>AAA</th> 
 
    <th>BBB</th> 
 
    </tr> 
 
    <tr> 
 
    <td>CCC</td> 
 
    <td>DDD</td> 
 
    </tr> 
 
    <tr style="color:red"> <!-- Here --> 
 
    <td>EEE</td> 
 
    <td>FFF</td> 
 
    </tr> 
 
    <tr> 
 
    <td>GGG</td> 
 
    <td>HHH</td> 
 
    </tr> 
 
</table>

+0

你是對全行打印爲紅色,我用像html_table + = 「」 +列+ ' \ n' 現在的表不匹配 – kitty

+0

你的意思是不匹配是什麼意思?我添加了一個例子來說明我是如何做到的 – frankyjuang