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

Zach
  • 18,594
  • 18
  • 59
  • 68

1 Answers1

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)
Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
Antoine P.
  • 4,181
  • 1
  • 24
  • 17
  • 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 Dec 29 '14 at 21:05
  • This work aslong as the original file can be encode as utf-8, for non utf-8 file it raise error :( – James Jul 30 '19 at 22:28