我正在尝试创建一个应用,用户可以单击一个按钮将CSV文件上传到Pandas(如果可以完成此工作,请在pd
中做更多的工作),然后显示数据。有人可以给我小费吗?我先向您道歉,如果它的基本知识,这里没有很多智慧:
app.py
from flask import Flask, make_response, request
import pandas as pd
app = Flask(__name__)
@app.route('/')
def form():
return """
<html>
<body>
<h1>Economizer Diagnostics</h1>
<form action="/transform" method="post" enctype="multipart/form-data">
<input type="file" name="data_file" />
<input type="submit" />
</form>
</body>
</html>
"""
@app.route('/transform', methods=["POST"])
def transform_view():
if request.method == 'POST':
df = pd.read_csv(request.files.get('file'), index_col='Date', parse_dates=True)
return render_template('simple.html', tables=[df.to_html(classes='data')], titles=df.columns.values)
return 'Oops, Try again something went wrong!'
if __name__ == '__main__':
app.run(debug=True)
simple.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }}
{% endfor %}
</body>
</html>
代码运行时,我收到有关熊猫CSV文件加载中无效文件路径的错误。
完整追踪:
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\benb\Desktop\Flask\uploadTest3.py", line 28, in transform_view
df = pd.read_csv(request.files.get('file'))
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 413, in _read
filepath_or_buffer, encoding, compression)
File "C:\Users\benb\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\common.py", line 232, in get_filepath_or_buffer
raise ValueError(msg.format(_type=type(filepath_or_buffer)))
ValueError: Invalid file path or buffer object type: <class 'NoneType'>
非软件专家在这里提供任何提示帮助。谢谢
答案 0 :(得分:0)
尝试一下:
# data_file is the name of the file upload field
f = request.files['data_file']
# for security - stops a hacker e.g. trying to overwrite system files
filename = secure_filename(f.filename)
# save a copy of the uploaded file
f.save(filename)
# And then use it ...
df = pd.read_csv(filename, index_col='Date', parse_dates=True)