EmptyDataError : No columns to parse from file

EmptyDataError : No columns to parse from file

我正在尝试在 VSCode 中编写一个 flask 代码,该代码读取 CSV 文件并将其作为 table 在浏览器上显示。 这是 .py 代码:

from flask import Flask,render_template,request
import os
import pandas as pd

app=Flask(__name__)
app.secret_key="123"

app.config["UPLOAD_FOLDER1"]="static/csv"

@app.route("/upload",methods=['GET','POST'])
def upload():
    return render_template("UploadCsv.html")

@app.route("/display",methods=["GET","POST"])
def display():
    upload_file = request.files['upload_csv']
    if upload_file.filename != '':
            file_path = os.path.join(app.config["UPLOAD_FOLDER1"], upload_file.filename)
            upload_file.save(file_path)
            data=pd.read_csv(upload_file,sep=",")
            return render_template("ExcelContent.html",data=data.to_html(index=False))

if __name__=='__main__':
    app.run(debug=True)

我在代码中使用了两个 HTML 文件,这些是: UploadCsv.html

<html>
    <head>
        <title>Upload CSV File</title>
    </head>
    <body>
        <div class="col-md-offset-3 col-md-5" style="margin-top:70px">
            <form method="POST" action="http://127.0.0.1:5000/display" enctype="multipart/form-data">
                <h3 class="page-header text-primary">Upload CSV File</h3>
                <div class="form-group">
                    <label>Browse CSV File</label>
                    <input type="file" class="form-control" name="upload_csv">
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-success btn-block">Upload CSV</button>
                </div>
            </form>
        </div>
    </body>
</html>

CsvContent.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSV File</title>
</head>
<body>
    <h2>Here's your uploaded CSV file :</h2>
    {{data|safe}}
</body>
</html>

现在,当我 运行 .py 并打开我的本地主机并上传 csv 时,会弹出上述错误。当我单击“提交”时,我的 csv 文件必须存储到的文件夹工作正常。显示部分是弹出此错误的地方。我不知道如何纠正。我的文件是一个简单的 CSV 文件(以逗号分隔)。这是错误消息的图片:

帮我解决错误,提前致谢!!

您可能应该将 filepath 传递给 pd.read_csv 方法,如下所示:

def display():
    upload_file = request.files['upload_csv']
    if upload_file.filename != '':
            file_path = os.path.join(app.config["UPLOAD_FOLDER1"], upload_file.filename)
            upload_file.save(file_path)
            data=pd.read_csv(file_path,sep=",")
            return render_template("ExcelContent.html",data=data.to_html(index=False))