#26040: Streaming Large CSV Files Example Incorrect
-----------------------------+---------------------------------------------
Reporter: przerull | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.8
Documentation |
Severity: Normal | Keywords: csv streaming documentation bug
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------------
Hi everyone,
The documentation has an example of how to stream large CSV files.
[https://docs.djangoproject.com/en/1.8/howto/outputting-csv/#streaming-
large-csv-files]
This is great but unfortunately the solution is incorrect (at least in
django 1.8 using python 3.4).
Per the documentation for the [https://docs.djangoproject.com/en/1.8/ref
/request-response/#django.http.StreamingHttpResponse StreamingHTTPResponse
class] "It should be given an iterator that yields strings as content."
but csvwriter.writerow returns None, not the result of the file.write call
of the file passed to the csvwriter. The Echo class provided in the
example was a good idea but it doesn't appear to work.
An alternative solution that does work would be:
{{{
def streaming_csv_writer(rows_to_output):
memory_file = StringIO()
writer = csv.writer(memory_file)
for row in rows_to_output:
writer.writerow(row)
memory_file.seek(0)
yield memory_file.read()
memory_file.truncate(0)
response = StreamingHTTPResponse(streaming_csv_writer(rows), ...)
}}}
I'm happy to patch this myself but I wanted to discuss it first before
writing the patch to get some additional opinions and to try to discover a
bit of the history of this documentation example (because I have a feeling
it must have worked at some time in the past).
Django is a great framework and I'm truly grateful to the maintainers and
contributors to the project. You folks rock!
--
Ticket URL: <https://code.djangoproject.com/ticket/26040>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.4d23870cbd0fe02c45778de4579d1fa8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.