2017-06-02 614 views
3

我試圖從.csv文件獲取數據並將其導入到python中的HTML表格中。在Python中將CSV轉換爲HTML表格

這是CSV文件https://www.mediafire.com/?mootyaa33bmijiq

語境:
的CSV會填充一個足球隊[年齡組,圓形,反對黨,球隊得分,反對黨得分,位置]數據。我需要能夠選擇一個特定的年齡組,並只在單獨的表格中顯示這些詳細信息。

這一切到目前爲止,我已經得到了....

infile = open("Crushers.csv","r") 

for line in infile: 
    row = line.split(",") 
    age = row[0] 
    week = row [1] 
    opp = row[2] 
    ACscr = row[3] 
    OPPscr = row[4] 
    location = row[5] 

if age == 'U12': 
    print(week, opp, ACscr, OPPscr, location) 
+0

您可以用'pandas'庫來實現這一目標。 'pandas'有一個名爲'to_html'的方法。這裏是鏈接https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html – arnold

回答

2

你開始打印所需的行之前,輸出一些HTML建立適當的表結構。

當您找到想要打印的行時,將其輸出爲HTML表格行格式。

# begin the table 
print("<table>") 

# column headers 
print("<th>") 
print("<td>Week</td>") 
print("<td>Opp</td>") 
print("<td>ACscr</td>") 
print("<td>OPPscr</td>") 
print("<td>Location</td>") 
print("</th>") 

infile = open("Crushers.csv","r") 

for line in infile: 
    row = line.split(",") 
    age = row[0] 
    week = row [1] 
    opp = row[2] 
    ACscr = row[3] 
    OPPscr = row[4] 
    location = row[5] 

    if age == 'U12': 
     print("<tr>") 
     print("<td>%s</td>" % week) 
     print("<td>%s</td>" % opp) 
     print("<td>%s</td>" % ACscr) 
     print("<td>%s</td>" % OPPscr) 
     print("<td>%s</td>" % location) 
     print("</tr>") 

# end the table 
print("</table>") 
5

首先安裝熊貓:

pip install pandas 

然後運行:

import pandas as pd 

columns = ['age', 'week', 'opp', 'ACscr', 'OPPscr', 'location'] 
df = pd.read_csv('Crushers.csv', names=columns) 

# This you can change it to whatever you want to get 
age_15 = df[df['age'] == 'U15'] 
# Other examples: 
bye = df[df['opp'] == 'Bye'] 
crushed_team = df[df['ACscr'] == '0'] 
crushed_visitor = df[df['OPPscr'] == '0'] 
# Play with this 

# Use the .to_html() to get your table in html 
print(crushed_visitor.to_html()) 

你會得到什麼如:

<table border="1" class="dataframe"> 
 
    <thead> 
 
    <tr style="text-align: right;"> 
 
     <th></th> 
 
     <th>age</th> 
 
     <th>week</th> 
 
     <th>opp</th> 
 
     <th>ACscr</th> 
 
     <th>OPPscr</th> 
 
     <th>location</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <th>34</th> 
 
     <td>U17</td> 
 
     <td>1</td> 
 
     <td>Banyo</td> 
 
     <td>52</td> 
 
     <td>0</td> 
 
     <td>Home</td> 
 
    </tr> 
 
    <tr> 
 
     <th>40</th> 
 
     <td>U17</td> 
 
     <td>7</td> 
 
     <td>Aspley</td> 
 
     <td>62</td> 
 
     <td>0</td> 
 
     <td>Home</td> 
 
    </tr> 
 
    <tr> 
 
     <th>91</th> 
 
     <td>U12</td> 
 
     <td>7</td> 
 
     <td>Rochedale</td> 
 
     <td>8</td> 
 
     <td>0</td> 
 
     <td>Home</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

0

下面的函數將filename,headers(可選)和delimiter(可選)作爲輸入,並將csv轉換爲html表並返回字符串。 如果未提供標題,則假定標題已存在於csv文件中。

csv文件內容到HTML格式的表格

def csv_to_html_table(fname,headers=None,delimiter=","): 
    with open(fname) as f: 
     content = f.readlines() 
    #reading file content into list 
    rows = [x.strip() for x in content] 
    table = "<table>" 
    #creating HTML header row if header is provided 
    if headers is not None: 
     table+= "".join(["<th>"+cell+"</th>" for cell in headers.split(delimiter)]) 
    else: 
     table+= "".join(["<th>"+cell+"</th>" for cell in rows[0].split(delimiter)]) 
     rows=rows[1:] 
    #Converting csv to html row by row 
    for row in rows: 
     table+= "<tr>" + "".join(["<td>"+cell+"</td>" for cell in row.split(delimiter)]) + "</tr>" + "\n" 
    table+="</table><br>" 
    return table 

在你的情況下轉換,函數調用看起來像:

filename="Crushers.csv" 
myheader='age,week,opp,ACscr,OPPscr,location' 
html_table=csv_to_html_table(filename,myheader)