(Feb-26-2018, 12:48 PM)Oliver Wrote: There must be a simple way to read csv "data" without writing an entire method like that.Have to follow the HTTP protocol and how framework dealing with files over net.
As you explain you want to send data as one variable(i guess this mean all content of csv?),the easiest way is to deal with it like file object.
Try to recreate data from
requests.vaules
will be difficult.You know that code over give the whole file uploaded
result.csv
So then can open it local with
pd.read_csv('result.csv')
.If sending back to a view could use tablib,which make a html table.
Example:
from flask import Flask, make_response, request import io, os import csv import tablib app = Flask(__name__) def transform(text_file_contents): return text_file_contents.replace("=", ",") @app.route('/') def form(): return """ <html> <body> <h1>Transfer a file demo</h1> <form action="/transform" method="post" enctype="multipart/form-data"> <input type="file" name="data_file" /> <input type="submit" /> </form> <br> <a href="/read_cvs">Read csv</a> </body> </html> """ @app.route('/transform', methods=["POST"]) def transform_view(): f = request.files['data_file'] if not f: return "No file" stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None) ''' csv_input = csv.reader(stream) for row in csv_input: print(row)''' stream.seek(0) result = transform(stream.read()) response = make_response(result) response.headers["Content-Disposition"] = "attachment; filename=result.csv" return response @app.route('/read_cvs', methods=["GET"]) def read_csv(): dataset = tablib.Dataset() with open(os.path.join(os.path.dirname(__file__),'C:/Users/Tom/Downloads/result.csv')) as f: dataset.csv = f.read() return dataset.html if __name__ == "__main__": app.run(debug=True)