sscanf needed

U

Uwe Mayer

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
 
F

Fredrik Lundh

Uwe said:
I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.
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>
 
L

Leif B. Kristensen

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.
 
K

Kent Johnson

Uwe said:
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
 
A

Andrew E

Uwe said:
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" )
 
L

Lee Harr

Uwe said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top