Browse files

[1.2.X] Fixed #14777 -- Added docs on working around the Python CSV m…

…odule's lack of Unicode support. Thanks to adamv for the report and draft patch.

Backport of [14749] from trunk.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 3c951d9 commit 3d07bb71c6ffc225e6d3a32975f6855b2561e85a Gabriel Hurley committed Nov 29, 2010
Showing with 25 additions and 2 deletions.
  1. +25 −2 docs/howto/outputting-csv.txt
@@ -56,12 +56,35 @@ mention:
about escaping strings with quotes or commas in them. Just pass
``writerow()`` your raw strings, and it'll do the right thing.
+Handling Unicode
+Python's ``csv`` module does not support Unicode input. Since Django uses
+Unicode internally this means strings read from sources such as
+:class:`~django.http.HttpRequest` are potentially problematic. There are a few
+options for handling this:
+ * Manually encode all Unicode objects to a compatible encoding.
+ * Use the ``UnicodeWriter`` class provided in the `csv module's examples
+ section`_.
+ * Use the `python-unicodecsv module`_, which aims to be a drop-in
+ replacement for ``csv`` that gracefully handles Unicode.
+For more information, see the Python `CSV File Reading and Writing`_
+.. _`csv module's examples section`:
+.. _`python-unicodecsv module`:
+.. _`CSV File Reading and Writing`:
Using the template system
Alternatively, you can use the :doc:`Django template system </topics/templates>`
-to generate CSV. This is lower-level than using the convenient CSV, but the
-solution is presented here for completeness.
+to generate CSV. This is lower-level than using the convenient Python ``csv``
+module, but the solution is presented here for completeness.
The idea here is to pass a list of items to your template, and have the
template output the commas in a :ttag:`for` loop.

0 comments on commit 3d07bb7

Please sign in to comment.