烧瓶将CSV导入 pandas df [英] Flask import CSV into pandas df

查看:41
本文介绍了烧瓶将CSV导入 pandas df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用,用户可以单击一个按钮将CSV文件上传到Pandas(如果可以完成此工作,请在 pd 中做更多的工作),然后显示数据.有人可以给我小费吗?我先向您道歉,如果它的基本知识,在这里没有很多智慧:

I am attempting to create an app where the user could click a button to upload a CSV file into Pandas (hoping to do more in pd if I can get this work) and then display the data. Can someone give me a tip? I apologize in advance if its basic stuff, there isnt a lot of wisdom here:

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文件加载中无效文件路径的错误.

When the code runs, I get an error about an invalid file path on the Pandas CSV file load.

完整追踪:

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'>

非软件专家在这里提供任何提示帮助.谢谢

Non software guy here any tips help. Thanks

推荐答案

尝试一下:

# 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)

这篇关于烧瓶将CSV导入 pandas df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆