By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,595 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,595 IT Pros & Developers. It's quick & easy.

sscanf needed

P: n/a
Hi,

I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.
Is there a shorter way than using regular expressions? Is there a sscanf
function as in C?

Thanks,
Uwe
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Uwe Mayer wrote:
I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.

import time
help(time.strptime)

Help on built-in function strptime in module time:

strptime(...)
strptime(string, format) -> struct_time

Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as
strftime()).

</F>

Jul 19 '05 #2

P: n/a
Uwe Mayer skrev:
Hi,

I've got a ISO 8601 formatted date-time string which I need to read
into a datetime object.


Look up time.strptime, it does exactly what you want.
--
Leif Biberg Kristensen
http://solumslekt.org/
Jul 19 '05 #3

P: n/a
Uwe Mayer wrote:
Hi,

I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.


Something like this (adjust the format to suit):
import datetime, time
dt = datetime.datetime(*time.strptime(data, "%Y-%m-%d %H:%M:%S")[:6])

Kent
Jul 19 '05 #4

P: n/a
Uwe Mayer wrote:
Hi,

I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.
Is there a shorter way than using regular expressions? Is there a sscanf
function as in C?


in addition to the other comments...

I like re, because it gives me the most control. See below.
import re
import datetime

class Converter:

def __init__( self ):
self.isoPattern = re.compile( "(\d\d\d\d)-(\d\d)-(\d\d)[tT
](\d\d):(\d\d):(\d\d)" )

def iso2date( self, isoDateString ):
match = self.isoPattern.match( isoDateString )
if not match: raise ValueError( "Not in ISO format: '%s'" %
isoDateString )

return datetime.datetime(
int(match.group(1)),
int(match.group(2)),
int(match.group(3)),
int(match.group(4)),
int(match.group(5)),
int(match.group(6))
)

c = Converter()
def demo( iso ):
try:
date = c.iso2date( iso )
print "Input '%s' -> datetime: %s" % ( iso, date )
except ValueError, e:
print str(e)

demo( "2005-04-21T12:34:56" )
demo( "2005-04-21 12:34:57" )
demo( "2005-04-2 12:34:57" )
Jul 19 '05 #5

P: n/a
On 2005-04-17, Andrew E <an****@nospam.com> wrote:
Uwe Mayer wrote:
Hi,

I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.
Is there a shorter way than using regular expressions? Is there a sscanf
function as in C?


in addition to the other comments...

I like re, because it gives me the most control. See below.
import re
import datetime

class Converter:

def __init__( self ):
self.isoPattern = re.compile( "(\d\d\d\d)-(\d\d)-(\d\d)[tT
](\d\d):(\d\d):(\d\d)" )

def iso2date( self, isoDateString ):
match = self.isoPattern.match( isoDateString )
if not match: raise ValueError( "Not in ISO format: '%s'" %
isoDateString )

return datetime.datetime(
int(match.group(1)),
int(match.group(2)),
int(match.group(3)),
int(match.group(4)),
int(match.group(5)),
int(match.group(6))
)

c = Converter()
def demo( iso ):
try:
date = c.iso2date( iso )
print "Input '%s' -> datetime: %s" % ( iso, date )
except ValueError, e:
print str(e)

demo( "2005-04-21T12:34:56" )
demo( "2005-04-21 12:34:57" )
demo( "2005-04-2 12:34:57" )

That's nice. We should get some code in to the module
so that it is simple to round-trip the default datetime
timestamps.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.