<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 23, 2014 at 7:41 PM, Shrayas rajagopal <span dir="ltr"><<a href="mailto:shrayasr@gmail.com" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=shrayasr@gmail.com&cc=&bcc=&su=&body=','_blank');return false;">shrayasr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Has any of you encountered this? Have you managed to fix it? Any help would be appreciated. Thanks.</blockquote></div><br><div class="gmail_extra">Guys,</div><div class="gmail_extra"><br></div><div class="gmail_extra">Seems like I solved the issue and also _kind of_ understood why it is happening.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Basically, the issue was caused because the line terminators are *expected* to</div><div class="gmail_extra">either be \r or \n. And saving it via excel seemed like it gave it a \r\n ending</div><div class="gmail_extra"><br></div><div class="gmail_extra">Firstly, I should thank Daniel who on the same[1] mailing list I linked in my </div><div class="gmail_extra">first mail, spoke about writing an iterator/generator and replacing the \r\n</div><div class="gmail_extra">with just \n on every line. </div><div class="gmail_extra"><br></div><div class="gmail_extra">With this, I just had a quick thought and tried it out. As the documentation for</div><div class="gmail_extra">FileStorage[2] indicates, It is possible to do a `stream.read()` to get the current</div><div class="gmail_extra">opened stream. </div><div class="gmail_extra"><br></div><div class="gmail_extra">So, all I did was to read the stream, split it at \r and pass it to the </div><div class="gmail_extra">csv.reader function and voila! It worked. Here is the same piece of code, in </div><div class="gmail_extra">working condition:</div><div class="gmail_extra"><br></div><div class="gmail_extra">[...]</div><div class="gmail_extra"><br></div><div class="gmail_extra">@app.route("/", methods=["POST"])</div><div class="gmail_extra">def index_post():</div><div class="gmail_extra">    payload = request.files["foobar"]</div><div class="gmail_extra">    lines = csv.DictReader(payload.read().split("\r"))</div><div class="gmail_extra">    for line in lines:</div><div class="gmail_extra">        print line</div><div class="gmail_extra">    return redirect(url_for("index"))</div><div class="gmail_extra"><br></div><div class="gmail_extra">[...]</div><div class="gmail_extra"><br></div><div class="gmail_extra">That was just a hunch though, I wanted to know why it worked and why it didn't</div><div class="gmail_extra">work initially and I ended up finding answers for both.</div><div class="gmail_extra"><br></div><div class="gmail_extra">1. Why it didn't work</div><div class="gmail_extra"><br></div><div class="gmail_extra">  Turns out there is this property called lineterminator[3] on the csv Dialect</div><div class="gmail_extra">  object that lets us specify a lineterminator. </div><div class="gmail_extra"><br></div><div class="gmail_extra">  But under that documentation theres a small box that says, and I quote: </div><div class="gmail_extra"><br></div><div class="gmail_extra">  "Note The reader is hard-coded to recognise either '\r' or '\n' as </div><div class="gmail_extra">  end-of-line, and ignores lineterminator. This behavior may change in the </div><div class="gmail_extra">  future."</div><div class="gmail_extra"><br></div><div class="gmail_extra">  So it wouldn't matter even if I set the lineterminator and gave the csv a </div><div class="gmail_extra">  new dialect. the reader would just ignore it. </div><div class="gmail_extra"><br></div><div class="gmail_extra">2. Why it worked</div><div class="gmail_extra"><br></div><div class="gmail_extra">  The second question was why it worked when I gave the csv.reader class a list?</div><div class="gmail_extra"><br></div><div class="gmail_extra">  Going back to the documentation for csv.reader[4], I found out that _csvfile_</div><div class="gmail_extra">  can accept any _Iterator_ object as its input. I quote: </div><div class="gmail_extra"><br></div><div class="gmail_extra">  "csvfile can be any object which supports the iterator protocol and returns a </div><div class="gmail_extra">  string each time its next() method is called — file objects and list objects </div><div class="gmail_extra">  are both suitable."</div><div class="gmail_extra"><br></div><div class="gmail_extra">So overall it seemed to me like the csv.reader was looking for a \n lineterminator</div><div class="gmail_extra">and saving it in Excel gave it a \r\n lineterminator which is why the initial</div><div class="gmail_extra">exception was being thrown. When I split it by the \r and passed it in to the </div><div class="gmail_extra">reader, it worked since the line endings would now have been simple \ns </div><div class="gmail_extra"><br></div><div class="gmail_extra">Hope this helps someone else. </div><div class="gmail_extra"><br></div><div class="gmail_extra">---</div><div class="gmail_extra">[1]: <a href="http://librelist.com/browser/flask/2013/7/18/filestorage-and-excel-file/#762ba54312006274ba80a09b7d59090d">http://librelist.com/browser/flask/2013/7/18/filestorage-and-excel-file/#762ba54312006274ba80a09b7d59090d</a></div><div class="gmail_extra">[2]: <a href="http://werkzeug.pocoo.org/docs/0.9/datastructures/#werkzeug.datastructures.FileStorage">http://werkzeug.pocoo.org/docs/0.9/datastructures/#werkzeug.datastructures.FileStorage</a></div><div class="gmail_extra">[3]: <a href="https://docs.python.org/2/library/csv.html#csv.Dialect.lineterminator">https://docs.python.org/2/library/csv.html#csv.Dialect.lineterminator</a></div><div class="gmail_extra">[4]: <a href="https://docs.python.org/2/library/csv.html#csv.reader">https://docs.python.org/2/library/csv.html#csv.reader</a></div><div><br></div></div></div>