2017-06-02 40 views
0

試圖創建幾個包含來自讀取csv文件的表格的網頁,並試圖定義一個函數來創建html網頁,而不是多次編寫相同的代碼。定義一個函數在python中生成html頁面

讀我的CSV文件:

infile = open("new.csv", "r") 
data = [] 
for line in infile: 
    cols = line.split(",") 
    Oposition = cols[0] 
    Winner = cols[1] 
    Margin = cols[2] 
    Ground = cols[3] 
    Year = cols[4] 
    pair = (Oposition, Winner, Margin, Ground, Year) 
    data.append(pair) 
infile.close() 

到目前爲止我的代碼是:

page = """<!DOCTYPE html> 
    <html> 
    <head> 
    <title>abc</title> 
    <style> 
h1 { 
    text-align: center; 
} 
body { 
    background-image: url("2014.png"); 
    background-repeat: no-repeat; 
    background-position: right top; 
    background-attachment: fixed; 
} 
</style> 
    </head> 
    <body> 
    <h1>{{heading}}</h1> 
    {{paragraph}} 
    <p><a href = "index.html">Back to main page</a></p> 
    <table border="1"><tr><th>Oposition</th><th>Winner</th><th>Margin</th><th>Ground</th><th>Year</th></tr> 

""" 

out1=page.format(heading = "2012 Stats", paragraph = "<p>aaaaaaa</p>") 
out2 = page.format(heading = "2013 Stats", paragraph = "<p>bbbbb</P>") 
out3 = page.format(heading = "2014 Stats", paragraph = "<p>cccc</P>") 
out4 = page.format(heading = "2015 Stats", paragraph = "<p>ddddCSK</p>") 

def write_html_file(filename, body): 
    out = open(filename, "w") 
    out.write(body) 

write_html_file("2012.html",out1) 
write_html_file("2013.html",out2) 
write_html_file("2014.html",out3) 
write_html_file("2015.html",out4) 

for r in data: 
    if ''.join(r[4].split()) == "2012": 
     Oposition = r[0] 
     Winner = r[1] 
     Margin = r[2] 
     Ground=r[3] 
     Year = r[4] 
     out1.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>") 
    elif ''.join(r[4].split()) == "2013": 
     Oposition = r[0] 
     Winner = r[1] 
     Margin = r[2] 
     Ground=r[3] 
     Year = r[4] 
     out2.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>") 
    elif ''.join(r[4].split()) == "2014": 
     Oposition = r[0] 
     Winner = r[1] 
     Margin = r[2] 
     Ground=r[3] 
     Year = r[4] 
     out3.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>") 
    elif ''.join(r[4].split()) == "2015": 
     Oposition = r[0] 
     Winner = r[1] 
     Margin = r[2] 
     Ground=r[3] 
     Year = r[4] 
     out4.write("<tr> <td>" + Oposition+ '</td><td> '+ Winner+'</td><td> '+Margin+'</td><td> '+Ground+' </td><td>'+ Year+ " </td></tr>") 

def output(a): 
    a.write("</table> </body>\n") 
    a.write("</html>\n") 
    a.close() 

output(out1) 
output(out2) 
output(out3) 
output(out4) 

我嘗試按年2012年,2013年,2014年,2015年,使表和HTML頁面,使其包含他們每個人。只是不知道。 任何幫助或其他選項?非常感謝 我得到一個錯誤消息說:

---> 25 page1 = page.format(heading = "2012 Stats", paragraph = "<p>aaaa</p>") 
KeyError: '\n text-align' 
+0

我收到此錯誤信息:---> 38 OUT1 = page.format(標題=「2012 Stats」,paragraph =「

aaaaa

」)KeyError:'\ n text-align' – Dush

+0

你看過類似'jinja2'的東西,你定義一個HTML模板,將它傳遞給一個上下文,然後構建爲你的文件? –

+0

它看起來像你的out2和out3以大寫閉合段落標記結束。 – brt

回答

0

簡答

Python的內置string format syntax是單paramthesis包圍的變量。這就是爲什麼'\ n text-align'被視爲一個關鍵,而它的目的是成爲一種CSS風格。

解決方案

您可以繼續逃避你的CSS代碼段,但是我不會推薦它,因爲HTML文本將無法讀取,而且將難以維持。

因此,請使用模板引擎來提供幫助。有很多模板引擎:Jinja2Mako ..等。由於我比較熟悉的第一個,讓我告訴你如何得到第1頁工作::

from jinja2 import Environment 

env = Environment() 
page_template = env.from_string(page) 
page1 = page_template(heading="2012 Stats", paragraph="<p>aaaaaaa</p>") 

而且你需要安裝的Jinja2 :

$ pip install jinja2 

替代解決方案

你可以用我的圖書館pyexcelpyexcel-text獲取爲您呈現的html表格。示例代碼:

import pyexcel as p 

sheet = p.get_sheet(file_name='new.csv') 
sheet.colnames = ['Oposition', 'Winner', 'Margin', 'Ground', 'Year'] 
sheet.name = "2012 Stats" 
print(sheet.html) 

要運行上面的代碼,你需要安裝這兩個額外的軟件包:

$ pip install pyexcel pyexcel-text 
+0

是的,我可以使它使用jinja2。非常感謝 – Dush

相關問題