I am trying to write some Python 3 code to process a csv file uploaded via a web form (using wsgi). I have managed to get the file to upload, but I am struggling to use Python's csv tools to process it. It seems to be to do with bytes vs strings.
Here is what I have tried:
import cgi, csv
form = cgi.FieldStorage(fp=environ['wsgi.input'],environ=environ)
upload = form['upload']
file = upload.file
data = csv.DictReader(file)
for line in data:
#Do stuff here to process csv file
It gets as far as "for line in data", and then I get the following error message:
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
So the problem is that my file is binary, but csv wants a string file, right? Any idea how I can fix this?
One possible workaround that occurs to me would be simply to read the lines of the file without using the csv module and process the data manually, which would work, but seems a bit faffy. It would be nice to use the functionality of Python's csv module if possible.
The web form from which the file is uploaded has the attribute
enctype="multipart/form-data"
which I gather is required for uploading files.