2017-05-28 14 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を入力するコードです(インデントが間違っています。正しいインデントコードが必要な場合は、リンクで共有します)。 <tr>はあなたが色にしたい行である

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">

私は以下のようなコードの下にexcuteます。

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

関連する問題