【问题标题】:Upload, read, write excel file in Python flask在 Python flask 中上传、读取、写入 excel 文件
【发布时间】:2017-10-25 06:36:20
【问题描述】:

我正在使用此代码要求用户上传文件,我希望将其读入数据帧。 然后这个数据框应该在页面上显示为输出。

我应该在返回中写什么,以完成这个?

from flask import Flask, request, jsonify
import flask_excel as excel
import pandas as pd

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        return jsonify({"result": request.get_array(field_name='file')})
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
    </form>
    '''

@app.route("/export", methods=['GET'])
def export_records():
    return 

if __name__ == "__main__":
    app.run()

【问题讨论】:

    标签: python excel flask


    【解决方案1】:

    我猜你想要的一个准系统版本就是这个。但这显然需要更多的工作。

    from flask import Flask, request, jsonify
    import pandas as pd
    
    app=Flask(__name__)
    
    @app.route("/upload", methods=['GET', 'POST'])
    def upload_file():
        if request.method == 'POST':
            print(request.files['file'])
            f = request.files['file']
            data_xls = pd.read_excel(f)
            return data_xls.to_html()
        return '''
        <!doctype html>
        <title>Upload an excel file</title>
        <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
        <form action="" method=post enctype=multipart/form-data>
        <p><input type=file name=file><input type=submit value=Upload>
        </form>
        '''
    
    @app.route("/export", methods=['GET'])
    def export_records():
        return 
    
    if __name__ == "__main__":
        app.run()
    

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多