[Fixed]-How can I access an uploaded file in universal-newline mode?

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)
👤Antoine P.

Leave a comment