python - 烧瓶将CSV导入熊猫df
问题描述
我正在尝试创建一个应用程序,用户可以单击一个按钮将 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>
代码运行时,我收到有关 Pandas 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'>
非软件人在这里任何提示帮助。谢谢
解决方案
尝试这个:
# 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)
推荐阅读
- node.js - 如何使用背景光栅图像将 svg 转换为 png(在 bash 或 nodejs 中)?
- delphi - 来自客户端中 RESTRequest 的参数未传递给服务器 - Delphi
- graphql - GraphQL 模式拼接 - 无法读取未定义的属性“args”
- php - 当我单击注册时,它会以文本形式打开 php 文件,而不是向数据库中添加值?
- image-processing - 使用霍夫曼树中的所有 ASCII 字符
- java - Java中的浮点数据类型精度
- php - 使用 Guzzle 测试 api 得到 403
- php - 试图保护我的 SQL 查询免受注入攻击并且没有任何运气
- r - 在R中获取值DataFrame
- java - 包含 EventHandler 的循环没有按预期为 javafx 工作?