Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

UnicodeDictWriter - write unicode strings out to Excel compatible CSV files using Python. Stuart Langridge and I spent quite a while this morning battling with Excel. The magic combination for storing unicode text in a CSV file such that Excel correctly reads it is UTF-16, a byte order mark and tab delimiters rather than commas.

2 comments

  1. Thanks for posting this; Excel CSVs are my nemesis.

    Adam Vandenberg - 20th August 2008 14:24 - #

  2. Sometimes ago I explained how to do it in PHP:

    $xls = "My file\tDelimite\tpar\tdes\ttabs\toi\r\nline 2\there you are\r\n";
    $xls = chr(255).chr(254).mb_convert_encoding($xls, 'UTF-16LE', 'UTF-8');
    

    Then you just output the $xls with the right headers. You need the multibyte extension of PHP, UTF-8 is the original encoding and excel needs UTF-16.

    Loïc d'Anterroches - 20th August 2008 19:46 - #

Sign in with OpenID

Auto-HTML: Line breaks are preserved; URLs will be converted in to links.

Manual XHTML: Enter your own, valid XHTML. Allowed tags are a, p, blockquote, ul, ol, li, dl, dt, dd, em, strong, dfn, code, q, samp, kbd, var, cite, abbr, acronym, sub, sup, br, pre

A django site