Writing list of dictionaries to CSV

Tim Chase python.list at tim.thechases.com
Wed May 6 21:22:05 EDT 2015


On 2015-05-06 23:31, Denis McMahon wrote:
> On Tue, 05 May 2015 22:32:28 -0700, Kashif Rana wrote:
> > thanks for the feedback. I think its problem with excel itself,
> > showing wrong value. Because when I opened the csv file in text
> > editor, I can see correct value but opening in excel showing
> > wrong value. What I can do to see correct in excel as well.
> 
> You need to format your CSV date into a date format that Excel 
> understands when it imports it.

That's part of the problem.  Excel is taking the OP's literal values
and interpreting them as dates (and formatting them in the way Excel
wants to) rather than accepting them as literal values.

As ChrisA posted earlier, you have to use Excel's Import
functionality (there are several ways to get this wizard, but not all
ways of opening a .csv trigger the wizard), then specify those
particular columns as "Text" rather than "General" (which
auto-detects as a Date).  This prevents Excel from overthinking the
problem, allowing Excel to import the text as-is like the OP wants.

> First thing to try would be to export some dates from excel as CSV
> and see what format excel puts them in.
> 
> The see if excel recognises them as dates when you re-import the
> same file.

If you create the formatting on such cells the way that the OP wants
them, then export, then re-import, Excel still mangles them since
the CSV spec doesn't preserve formatting.

-tkc







More information about the Python-list mailing list