HTML 是普通字符串。您可以使用字符串函数创建带有HTML 的字符串,数据在<table> 中,并保存在文件中以在Web 浏览器中显示。
使用for-loop 将每一行数据转换为带有<tr> 的字符串,并添加到HTML 的其余部分。
您也可以使用Jinja 将文件与模板一起使用(例如在Web 框架中Flask),但它仍然需要模板内的for-loop 来生成行。
这样您就可以完全控制HTML。
你也可以在pandas中使用to_html
df = pandas.read_csv(filename, ...)
html = df.to_html()
只有<table>才能获得HTML,您可以将其保存在文件中并显示,也可以添加到其他HTML。
to_html() 有一些选项可以更改 <table> 中的某些内容,但您可能无法完全控制此表。
编辑:
import pandas as pd
df = pd.read_csv("file.csv")
df.to_html("file.html")
# or
table = df.to_html()
print(table)
html = "<html> <head></head> <body>" + table + "</body> </html>"
fp = open("file.html", "w")
fp.write(html)
fp.close()
编辑:
如果您在 China.csv、UK.csv 等分隔文件中有数据,请使用 for-loop
`# --- before loop ---
HTML = "<html> <head><title>All Countries</title> </head> <body>"
# --- loop ---
for country in ['China', 'UK', 'Italy']:
df = pd.read_csv( country + '.csv' )
table = df.to_html()
HTML += "<h1>" + country + "</h1>" + table
# --- after loop ---
HTML += "</body> </html>"
#print(HTML)
fp = open("output.html", "w")
fp.write(HTML)
fp.close()
如果您在文件夹中拥有所有 CSV 文件,即。 data 那么你可以使用os.listdir() 或glob.glob()
# --- before loop ---
HTML = "<html> <head><title>All Countries</title> </head> <body>"
folder = 'data'
# --- loop ---
for filename in sorted(os.listdir(folder)):
if filename.endswith('.csv'):
country = filename.replace('.csv', '')
fullpath = os.path.join(folder, filename)
df = pd.read( fullpath )
table = df.to_html()
HTML += "<h1>" + country + "</h1>" + table
# --- after loop ---
HTML += "</body> </html>"
#print(HTML)
fp = open("output.html", "w")
fp.write(HTML)
fp.close()
编辑:
使用字符串函数创建带有表格的 HTML。
import pandas as pd
# --- example data ---
df = pd.DataFrame({
'A': [ 1, 2, 3],
'B': ['X', 'Y', 'Z'],
'C': [0.1, 0.2, 0.3],
})
# --- before loop ---
HTML = """<!DOCTYPE html>
<html>
<head>
<title>Example Data</title>
</head>
<body>
"""
# --- loop ---
HTML += '<table>\n'
# headers
HTML += ' <tr>\n'
for header in df.columns:
HTML += ' <th>' + str(header) + '</th>\n'
HTML += ' </tr>\n'
# rows
for index, row in df.iterrows():
HTML += ' <tr>\n'
for cell in row:
HTML += ' <td>' + str(cell) + '</td>\n'
HTML += ' </tr>\n'
HTML += '</table>\n'
# --- after loop ---
HTML += """
</body>
</html>"""
# --- end ---
print(HTML)
结果:
<!DOCTYPE html>
<html>
<head>
<title>Example Data</title>
</head>
<body>
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<tr>
<td>1</td>
<td>X</td>
<td>0.1</td>
</tr>
<tr>
<td>2</td>
<td>Y</td>
<td>0.2</td>
</tr>
<tr>
<td>3</td>
<td>Z</td>
<td>0.3</td>
</tr>
</table>
</body>
</html>
同样使用pandas to_html()
import pandas as pd
# --- example data ---
df = pd.DataFrame({
'A': [ 1, 2, 3],
'B': ['X', 'Y', 'Z'],
'C': [0.1, 0.2, 0.3],
})
# --- before loop ---
HTML = """<!DOCTYPE html>
<html>
<head>
<title>Example Data</title>
</head>
<body>
"""
# --- loop ---
HTML += df.to_html()
# --- after loop ---
HTML += """
</body>
</html>"""
# --- end ---
print(HTML)
结果:
<!DOCTYPE html>
<html>
<head>
<title>Example Data</title>
</head>
<body>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>X</td>
<td>0.1</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>Y</td>
<td>0.2</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>Z</td>
<td>0.3</td>
</tr>
</tbody>
</table>
</body>
</html>