在 Python 中将 CSV 转换为 HTML 表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44320329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting CSV to HTML Table in Python
提问by kmarshy
I'm trying to take data from a .csv file and importing into a HTML table within python.
我正在尝试从 .csv 文件中获取数据并导入到 python 中的 HTML 表中。
This is the csv file https://www.mediafire.com/?mootyaa33bmijiq
这是 csv 文件https://www.mediafire.com/?mootyaa33bmijiq
Context:
The csv is populated with data from a football team [Age group, Round, Opposition, Team Score, Opposition Score, Location]. I need to be able to select a specific age group and only display those details in separate tables.
上下文:
csv 填充有来自足球队的数据 [年龄组、轮次、反对派、球队得分、反对派得分、位置]。我需要能够选择一个特定的年龄组,并且只在单独的表格中显示这些详细信息。
This is all I've got so far....
这就是我到目前为止所拥有的......
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)
采纳答案by John Gordon
Before you begin printing the desired rows, output some HTML to set up an appropriate table structure.
在开始打印所需的行之前,输出一些 HTML 以设置适当的表结构。
When you find a row you want to print, output it in HTML table row format.
当您找到要打印的行时,以 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>")
回答by Nabil Bennani
First install pandas:
首先安装熊猫:
pip install pandas
Then run:
然后运行:
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())
You'll get something like:
你会得到类似的东西:
<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>
回答by Messa
First some imports:
首先是一些进口:
import csv
from html import escape
import io
Now the building blocks - let's make one function for reading the CSV and another function for making the HTML table:
现在构建块 - 让我们创建一个用于读取 CSV 的函数和另一个用于制作 HTML 表格的函数:
def read_csv(path, column_names):
with open(path, newline='') as f:
# why newline='': see footnote at the end of https://docs.python.org/3/library/csv.html
reader = csv.reader(f)
for row in reader:
record = {name: value for name, value in zip(column_names, row)}
yield record
def html_table(records):
# records is expected to be a list of dicts
column_names = []
# first detect all posible keys (field names) that are present in records
for record in records:
for name in record.keys():
if name not in column_names:
column_names.append(name)
# create the HTML line by line
lines = []
lines.append('<table>\n')
lines.append(' <tr>\n')
for name in column_names:
lines.append(' <th>{}</th>\n'.format(escape(name)))
lines.append(' </tr>\n')
for record in records:
lines.append(' <tr>\n')
for name in column_names:
value = record.get(name, '')
lines.append(' <td>{}</td>\n'.format(escape(value)))
lines.append(' </tr>\n')
lines.append('</table>')
# join the lines to a single string and return it
return ''.join(lines)
Now just put it together :)
现在把它放在一起:)
records = list(read_csv('Crushers.csv', 'age week opp ACscr OPPscr location'.split()))
# Print first record to see whether we are loading correctly
print(records[0])
# Output:
# {'age': 'U13', 'week': '1', 'opp': 'Waterford', 'ACscr': '22', 'OPPscr': '36', 'location': 'Home'}
records = [r for r in records if r['age'] == 'U12']
print(html_table(records))
# Output:
# <table>
# <tr>
# <th>age</th>
# <th>week</th>
# <th>opp</th>
# <th>ACscr</th>
# <th>OPPscr</th>
# <th>location</th>
# </tr>
# <tr>
# <td>U12</td>
# <td>1</td>
# <td>Waterford</td>
# <td>0</td>
# <td>4</td>
# <td>Home</td>
# </tr>
# <tr>
# <td>U12</td>
# <td>2</td>
# <td>North Lakes</td>
# <td>12</td>
# <td>18</td>
# <td>Away</td>
# </tr>
# ...
# </table>
A few notes:
一些注意事项:
csv.reader
works better than line splitting because it also handles quoted values and even quoted values with newlineshtml.escape
is used to escape strings that could potentially contain character<
or>
it is often times easier to worh with dicts than tuples
usually the CSV files contain header (first line with column names) and could be easily loaded using
csv.DictReader
; but theCrushers.csv
has no header (the data start from very first line) so we build the dicts ourselves in the functionread_csv
both functions
read_csv
andhtml_table
are generalised so they can work with any data, the column names are not "hardcoded" into themyes, you could use pandas
read_csv
andto_html
instead :) But it is good to know how to do it without pandas in case you need some customization. Or just as a programming exercise.
csv.reader
比行拆分效果更好,因为它还处理引用值,甚至用换行符处理引用值html.escape
用于转义可能包含字符<
或>
使用 dicts 通常比使用元组更容易
通常 CSV 文件包含标题(带有列名的第一行)并且可以使用
csv.DictReader
; 但是Crushers.csv
没有标题(数据从第一行开始)所以我们在函数中自己构建字典read_csv
两者的功能
read_csv
和html_table
泛化,使他们可以与任何数据的工作,列名不“硬编码”到他们是的,你可以使用熊猫
read_csv
和to_html
代替:)不过这是好事,知道如何做没有的情况下,你需要一些定制大熊猫。或者只是作为一个编程练习。
回答by Davor Banovic
This should be working as well:
这也应该有效:
from html import HTML
import csv
def to_html(csvfile):
H = HTML()
t=H.table(border='2')
r = t.tr
with open(csvfile) as csvfile:
reader = csv.DictReader(csvfile)
for column in reader.fieldnames:
r.td(column)
for row in reader:
t.tr
for col in row.iteritems():
t.td(col[1])
return t
and call the function by passing the csv file to it.
并通过将 csv 文件传递给它来调用该函数。
回答by Yash
Below function takes filename, headers(optional) and delimiter(optional) as input and converts csv to html table and returns as string. If headers are not provided, assumes header is already present in csv file.
下面的函数将文件名、标题(可选)和分隔符(可选)作为输入,并将 csv 转换为 html 表并作为字符串返回。如果未提供标头,则假定标头已存在于 csv 文件中。
Converts csv file contents to HTML formatted table
将 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
In your case, function call will look like this, but this will not filterout entries in csv but directly convert whole csv file to HTML table.
在您的情况下,函数调用将如下所示,但这不会过滤掉 csv 中的条目,而是直接将整个 csv 文件转换为 HTML 表。
filename="Crushers.csv"
myheader='age,week,opp,ACscr,OPPscr,location'
html_table=csv_to_html_table(filename,myheader)
Note: To filter out entries with certain values add conditional statement in for loop.
注意:要过滤掉具有特定值的条目,请在 for 循环中添加条件语句。