16

I am working with a file uploaded using Django's forms.FileField. This returns an object of type InMemoryUploadedFile.

I need to access this file in universal-newline mode. Any ideas on how to do this without saving and then reopening the file?

Thanks

1 Answer 1

18

If you are using Python 2.6 or higher, you can use the io.StringIO class after having read your file into memory (using the read() method). Example:

>>> import io
>>> s = u"a\r\nb\nc\rd"
>>> sio = io.StringIO(s, newline=None)
>>> sio.readlines()
[u'a\n', u'b\n', u'c\n', u'd']

To actually use this in your django view, you may need to convert the input file data to unicode:

stream = io.StringIO(unicode(request.FILES['foo'].read()), newline=None)
2
  • This works great. I use it along with csv.DictReader in my django view. reader = csv.DictReader(stream) then for row in reader: #import each row Any thoughts on what this will do with memory usage? I'm on a 'budget' :) I know I should probably just save the file in a temp location but am curious if for the time being I need to do any garbage-like clean up?
    – teewuane
    Commented Dec 29, 2014 at 21:05
  • This work aslong as the original file can be encode as utf-8, for non utf-8 file it raise error :(
    – Nhu Trinh
    Commented Jul 30, 2019 at 22:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.