Skip to content
Advertisement

Django FileField not cooperating with Python 3’s csv module

I have this form:

JavaScript

The intended purpose of import_csv() is to import the CSV to the application’s database, but it has been altered for brevity’s sake.

An exception occurs when I attempt to iterate over school_csv, which I guess is when DictReader first attempts to read the file:

JavaScript

I don’t believe that I was presented with the opportunity to ever choose the mode to open the file with. How am I able to open in text mode so the csv module will handle it?

I’ve noticed that InMemoryUploadedFile has a file attribute which seemed useful, but it’s a io.BytesIO object. I imagine I could do some magic involving manually specifying the encoding.etc but I imagined that this is the sort of thing that Django would make easier.

What is the best way to approach this?

Advertisement

Answer

Try using

JavaScript

if it expects str or

JavaScript

if it wants StringIO.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement