使用python熊猫在网页文件中显示csv到占位符
问题描述:
我有一个csv文件,需要将其转换为在html中查看它。我知道蟒蛇大熊猫可以使用python熊猫在网页文件中显示csv到占位符
df = pd.read_csv("myfile.csv")
df.to_html('output.html')'
做,但我不希望它显示在一个网页,我已经index.html文件和我想表明它在那里,但在另一部分。
答
发现回答使用tablib
from flask import Flask, render_template
import tablib
import os
app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'email.csv')) as f:
dataset.csv = f.read()
@app.route("/")
def index():
data = dataset.html
#return dataset.html
return render_template('index.html', data=data)
if __name__ == "__main__":
app.run()
的index.html
<html>
<head>
<meta charset="utf-8" />
<link rel=stylesheet type=text/css href="{{ url_for('static',
filename='css/style.css') }}"/>
<title>Show CSV</title>
</head>
<body>
<div class="table">
{% block body %}
{{ data|safe }}
{% endblock %}
</div>
</body>
</html>
单程它使用'.load()'中的jquery加载output.html成从index.html的那部http://api.jquery.com/load/ –
使用分片行来获得你想要的'outputx.html',例如'df [10:21]将选择10-20行。 – swatchai