首页 > 解决方案 > 将html页面中选择的文件加载到pandas

问题描述

我正在尝试自动化 Winross(市场研究工具)表语法以生成开放式表。我需要为此使用两个 excel 文件,我打算为此使用 Pandas。我不确定如何将 excel 文件加载到使用 input type =“file”在 HTML 中选择的 pandas 中。

在下面添加 HTML 和 python 代码

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>OE Tables</title>
    </head>

    <body>
        <form class="" action="getfile" method="post" enctype="multipart/form-data">
            <h1>OE Tables Generator</h1>
            <h3>Select OE data:</h3>
            <input type="file" name="myfile" value=""><br><br>
            <input type="submit" name="" value="Upload OE File"><br><br>

            <h3>Select Code-frame:</h3>
            <input type="file" name="myfile" value=""><br><br>
            <input type="submit" name="" value="Upload Code-frame"><br <br>
        </form>
    </body>
</html>
from flask import Flask, render_template, request
import pandas as pd

app = Flask(__name__)


@app.route("/")
def index():
    return render_template("index.html")


@app.route('/getfile', methods=['GET', 'POST'])
def getfile():
    if request.method == 'POST':
        excel_file = request.files['myfile']
        oedata = pd.read_excel(excel_file)
        return oedata.head()

    else:
        result = request.args.get['myfile']
    return result

当前得到一个页面,错误如下: Internal Server Error 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。

标签: pythonhtmlpandasflask

解决方案


推荐阅读