Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning values from .writerow() #33

Merged
merged 1 commit into from Dec 29, 2014
Merged

Returning values from .writerow() #33

merged 1 commit into from Dec 29, 2014

Conversation

jdotjdot
Copy link
Contributor

In my Django project, I ran into the (admittedly niche) use case of needing to stream a large CSV file. Django offers a tutorial about how to do this, and it involves creating a fake buffer of sorts to return values from the csv writer rather than write to the actual buffer:

class Echo(object):
    """An object that implements just the write method of the file-like
    interface.
    """
    def write(self, value):
        """Write the value by returning it, instead of storing in a buffer."""
        return value

def some_streaming_csv_view(request):
    """A view that streams a large CSV file."""
    # Generate a sequence of rows. The range is based on the maximum number of
    # rows that can be handled by a single sheet in most spreadsheet
    # applications.
    rows = (["Row {0}".format(idx), str(idx)] for idx in range(65536))
    pseudo_buffer = Echo()
    writer = csv.writer(pseudo_buffer)
    response = StreamingHttpResponse((writer.writerow(row) for row in rows),
                                     content_type="text/csv")
    response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
    return response

I have non-ASCII characters in my data and needed to use the unicodecsv module and noticed that unicodecsv's writerow() method did not return anything. Adding a return statement fixed my problem. Even if only to better match the native Python csv module, I think this should be added.

Note that writerows() still doesn't properly return anything, but it also does not in the native Python csv module, and so I've left it for consistency's sake.

@jdotjdot
Copy link
Contributor Author

By the way, the test failures here are part of the main code, not anything I added--it's an except: syntax issue for Python 3:

image

@mic159
Copy link

mic159 commented Dec 23, 2014

Yes, even master is failing. PR #26 fixes that.

jdunck added a commit that referenced this pull request Dec 29, 2014
Returning values from .writerow(), Thanks @jdotjdot
@jdunck jdunck merged commit 1558da7 into jdunck:master Dec 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants