Last active
March 20, 2024 17:41
-
-
Save jasco/515c0847504e67002b64c10dfd75259d to your computer and use it in GitHub Desktop.
Python3 Flask CSV parsing of uploaded file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python3 implementation for CSV parsing of uploaded file | |
# Test: curl -H "Content-Type: multipart/form-data" -F "[email protected]" http://localhost:5000/myroute | |
import csv | |
import codecs | |
from flask import (jsonify, request) | |
# ... | |
@app.route('/myroute', methods=['POST']) | |
def myroute(): | |
flask_file = request.files['file'] | |
if not flask_file: | |
return 'Upload a CSV file' | |
data = [] | |
stream = codecs.iterdecode(flask_file.stream, 'utf-8') | |
for row in csv.reader(stream, dialect=csv.excel): | |
if row: | |
data.append(row) | |
return jsonify(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is an error on this gist.
stream = codecs.iterdecode(flask_file.stream, 'utf-8')
instead of
stream = codecs.codecs.iterdecode(flask_file.stream, 'utf-8')
thanks anyways