Writing list of dictionaries to CSV

Ian Kelly ian.g.kelly at gmail.com
Wed May 6 14:27:31 EDT 2015


On Wed, May 6, 2015 at 12:22 PM, Tim Chase
<python.list at tim.thechases.com> wrote:
> On 2015-05-06 19:08, MRAB wrote:
>> You could tell it to quote any value that's not a number:
>>
>>       w = csv.DictWriter(f, pol_keys,
>> quoting=csv.QUOTE_NONNUMERIC)
>>
>> It looks like all of the values you have are strings, so they'll
>> all be quoted.
>>
>> I would hope that Excel will then treat it as a string; it would be
>> stupid if it didn't! :-)
>
> Sadly, Excel *is* that stupid based on the tests I tried just now. :-(
>
> Regardless of whether "Mar 2015" is quoted or unquoted in the source
> CSV file, Excel tries to outwit you and mangles the presentation.

Quoting a value in csv doesn't mean it's a string; it just means that
it's a single field.

You *can* force Excel to treat a value as a string by prefixing it
with an apostrophe, though.



More information about the Python-list mailing list