sscanf needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Uwe Mayer

    #1

    sscanf needed

    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
  • Fredrik Lundh

    #2
    Re: sscanf needed

    Uwe Mayer wrote:
    [color=blue]
    > I've got a ISO 8601 formatted date-time string which I need to read into a
    > datetime object.[/color]
    [color=blue][color=green][color=darkred]
    >>> import time
    >>> help(time.strpt ime)[/color][/color][/color]
    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>

    Comment

    • Leif B. Kristensen

      #3
      Re: sscanf needed

      Uwe Mayer skrev:
      [color=blue]
      > Hi,
      >
      > I've got a ISO 8601 formatted date-time string which I need to read
      > into a datetime object.[/color]

      Look up time.strptime, it does exactly what you want.
      --
      Leif Biberg Kristensen

      Comment

      • Kent Johnson

        #4
        Re: sscanf needed

        Uwe Mayer wrote:[color=blue]
        > Hi,
        >
        > I've got a ISO 8601 formatted date-time string which I need to read into a
        > datetime object.[/color]

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

        Kent

        Comment

        • Andrew E

          #5
          Re: sscanf needed

          Uwe Mayer wrote:[color=blue]
          > 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?[/color]

          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.dateti me(
          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" )


          Comment

          • Lee Harr

            #6
            Re: sscanf needed

            On 2005-04-17, Andrew E <andrew@nospam. com> wrote:[color=blue]
            > Uwe Mayer wrote:[color=green]
            >> 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?[/color]
            >
            > 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.dateti me(
            > 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" )
            >
            >[/color]


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

            Comment

            Working...