Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > The fastest way to convert a long list of date

Reply
Thread Tools

The fastest way to convert a long list of date

 
 
loredana.pier@gmail.com
Guest
Posts: n/a
 
      02-08-2009
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’,................]

Thank you

Loredana
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-08-2009
On Sun, Feb 8, 2009 at 4:46 AM, <> 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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-08-2009
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’,................]


For large source lists the following should be quite fast...

months = "jan feb mar apr may jun jul aug sep oct dec".split()
lookup = dict(("%02d-%02d" % (mi+1, d), "%02d-%s" % (d, m)) for mi, m in
enumerate(months) for d in range(1, 32))

source = ["2008-09-26", "2008-09-28", "2008-09-29"]
print [lookup[d[-5:]] for d in source]


.... but it is also very brittle. Various compromises are possible, but to
find the best would require that you tell us a bit more about your use
case. In my experience people are a bit too fast to demand "fast".

Peter
 
Reply With Quote
 
loredana.pier@gmail.com
Guest
Posts: n/a
 
      02-08-2009
First of all: Thanks for your reply
My use case is the following:
I perform a query to DB and the result of query is this;

>>> print rows

{‘Label’: {'2009:01:30': 9, '2009:01:28': 13, '2009:01:29': 19,
'2009:01:26': 1, '2009:01:27': 3, '2009:01:20': 86, '2009:01:22': 3,
'2009:01:23': 12, '2009:02:07': 3, '2009:02:06': 12, '2009:02:05': 15,
'2009:02:04': 12, '2009:02:02': 2}}

I want to make a chart with this data using JSON format where:
• the key ‘Label’ is the title of chart,
• rows['Label'].keys() is x values,
• rows[‘Label’].values() is the y values

So my goal is:

1) to fill the bin of chart with no data (for example in this tupla..
[.., '2009:01:28: 9, '2009:01:30: 13,..].. the date '2009:01:29’ is
missing and I want to append the value ['2009:01:29’,0] to rows
[‘Label’])

2) convert the date format (for example in '%A %d, %b %y')

3) transform rows dictionary in JSON code

The point 3) is easy to implement but I’m wondering which is the best
way to implement the point 1) and 2) ...considering it will be used
for a web application (CherryPy)?

thanks,

Loredana
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fastest way to convert sql result into a dict or list ? rewonka@gmail.com Python 4 10-30-2008 08:24 AM
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
Fastest way to convert a byte of integer into a list Godzilla Python 15 07-15-2007 02:37 AM
Fastest 5 mp Digital Camera ? Fastest 4 mp Digital Camera? photoguysept102004@yahoo.com Digital Photography 6 10-28-2004 11:33 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57