Skip to content

Instantly share code, notes, and snippets.

@jasco
Last active March 20, 2024 17:41
Show Gist options
  • Save jasco/515c0847504e67002b64c10dfd75259d to your computer and use it in GitHub Desktop.
Save jasco/515c0847504e67002b64c10dfd75259d to your computer and use it in GitHub Desktop.
Python3 Flask CSV parsing of uploaded file
# Python3 implementation for CSV parsing of uploaded file
# Test: curl -H "Content-Type: multipart/form-data" -F "file=@mydata.csv" 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)
@kjimenezdev
Copy link

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

@jasco
Copy link
Author

jasco commented Oct 10, 2019

@kjimenezdev thanks. Fixed.

@thiagotancredi
Copy link

Oh man, thanks. It helped me a lot.

@iml1111
Copy link

iml1111 commented Mar 27, 2021

Wow, thanks!!

@artem-sitd
Copy link

thank for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment