The fastest way to convert a long list of date

Chris Rebert clp2 at rebertia.com
Sun Feb 8 08:05:33 EST 2009


On Sun, Feb 8, 2009 at 4:46 AM,  <loredana.pier at gmail.com> wrote:
> If I want to convert a single date format I can do:
>
> import datetime, dateutil.parser
> d = dateutil.parser.parse('2008-09-26)
> print d.strftime('%A %d, %b %y' )
>
> but if I want convert a long list of time how can do it in the fastest
> way?
> for example:
> from ['2008-09-26', '2008-09-28', '2008-09-29',.............]
> to ['26 sep','28 sep','29 sep',................]

import datetime
from dateutil.parser import parse
orig_dates = ['2008-09-26', '2008-09-28', ...]#etc
converted_dates = [parse(datestr).strftime('%A %d, %b %y' ) for
datestr in orig_dates]

Or if an iterator would be okay instead of a list, you can substitute
the list comprehension for itertools.imap():

#setup same as before
from itertools import imap
converted_dates = imap(lambda datestr: parse(datestr).strftime('%A %d,
%b %y' ), orig_dates)

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list