parsing a date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kalle Anke

    #1

    parsing a date

    I want to parse a date string, for example '2005-09-23', and since I haven't
    done this before I would like to ask what is the best way to do it.

    I've looked around and the dateutil seems to be what most people use, but
    unfortunately I only get an empty file when I try to download it. I also
    tried the standard modules and ended up with this

    import datetime
    from time import strptime

    d = '2005-09-23'
    w = strptime(d,'%Y-%m-%d')
    print datetime.date( w[0], w[1], w[2] )

    But I suspect there is a better way to do it??

  • Larry Bates

    #2
    Re: parsing a date

    Better than a single line of code? What is it that
    you are looking for?

    If you dates are consistent you can do:

    year, month, day=map(int, d.split('-'))

    but I'm not sure it is "better". I guess it depends
    on what you want to do with them after parsing.

    -Larry Bates

    Kalle Anke wrote:[color=blue]
    > I want to parse a date string, for example '2005-09-23', and since I haven't
    > done this before I would like to ask what is the best way to do it.
    >
    > I've looked around and the dateutil seems to be what most people use, but
    > unfortunately I only get an empty file when I try to download it. I also
    > tried the standard modules and ended up with this
    >
    > import datetime
    > from time import strptime
    >
    > d = '2005-09-23'
    > w = strptime(d,'%Y-%m-%d')
    > print datetime.date( w[0], w[1], w[2] )
    >
    > But I suspect there is a better way to do it??
    >[/color]

    Comment

    • Peter

      #3
      Re: parsing a date

      Kalle Anke wrote:
      [color=blue]
      >I want to parse a date string, for example '2005-09-23', and since I haven't
      >done this before I would like to ask what is the best way to do it.
      >
      >I've looked around and the dateutil seems to be what most people use, but
      >unfortunatel y I only get an empty file when I try to download it. I also
      >tried the standard modules and ended up with this
      >
      >import datetime
      >from time import strptime
      >
      >d = '2005-09-23'
      >w = strptime(d,'%Y-%m-%d')
      >print datetime.date( w[0], w[1], w[2] )
      >[/color]
      Well, thats not _to_ bad.
      [color=blue]
      >But I suspect there is a better way to do it??
      >
      >
      >[/color]
      There is...

      I would check out "strftime"
      With strftime you could do something like this...

      # ------
      print strftime("%Y-%m-%d")
      # -----

      or if you wanted to get the time in the past or future you could do
      something like

      # ------
      tm = time.localtime( time.time() + ((60 * 60) * 24))
      print strftime("%Y-%m-%d", tm)
      # -----

      Which gets the date of tommarow.

      HTH,
      Peter

      Comment

      • Kalle Anke

        #4
        Re: parsing a date

        On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote
        (in article <htadnfhhQL628K neRVn-gw@comcast.com> ):
        [color=blue]
        > but I'm not sure it is "better". I guess it depends
        > on what you want to do with them after parsing.[/color]


        Sorry, I should have been clearer. I want to parse the date and create a
        'date object' that is a part of larger object (I'm parsing a text file that
        represents the larger object and the date is a part of it).

        So my question should probably be: is there a better way to parse the date
        and generate the 'date object' than the two step

        w = strptime(d,'%Y-%m-%d')
        datetime.date( w[0], w[1], w[2] )

        Since I'm new to many things in Python I'm trying to learn if there is a
        "better" way of doing things.

        jem

        Comment

        • Peter Hansen

          #5
          Re: parsing a date

          Kalle Anke wrote:[color=blue]
          > On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote:
          >[color=green]
          >>but I'm not sure it is "better". I guess it depends
          >>on what you want to do with them after parsing.[/color]
          >
          > Sorry, I should have been clearer. I want to parse the date and create a
          > 'date object' that is a part of larger object (I'm parsing a text file that
          > represents the larger object and the date is a part of it).
          >
          > So my question should probably be: is there a better way to parse the date
          > and generate the 'date object' than the two step
          >
          > w = strptime(d,'%Y-%m-%d')
          > datetime.date( w[0], w[1], w[2] )
          >
          > Since I'm new to many things in Python I'm trying to learn if there is a
          > "better" way of doing things.[/color]

          You're still not defining what "better" means to you, so who can say?

          Perhaps you think a single line would be "better"?

          datetime.date(* time.strptime(d , '%Y-%m-%d')[:3])

          -Peter

          Comment

          • Kalle Anke

            #6
            Re: parsing a date

            On Sat, 24 Sep 2005 16:06:06 +0200, Peter Hansen wrote
            (in article <04OdncFaFO3RwK jenZ2dnUVZ_s6dn Z2d@powergate.c a>):
            [color=blue]
            > Kalle Anke wrote:[color=green]
            >> On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote:
            >>[color=darkred]
            >>> but I'm not sure it is "better". I guess it depends
            >>> on what you want to do with them after parsing.[/color]
            >>
            >> Sorry, I should have been clearer. I want to parse the date and create a
            >> 'date object' that is a part of larger object (I'm parsing a text file that
            >> represents the larger object and the date is a part of it).
            >>
            >> So my question should probably be: is there a better way to parse the date
            >> and generate the 'date object' than the two step
            >>
            >> w = strptime(d,'%Y-%m-%d')
            >> datetime.date( w[0], w[1], w[2] )
            >>
            >> Since I'm new to many things in Python I'm trying to learn if there is a
            >> "better" way of doing things.[/color]
            >
            > You're still not defining what "better" means to you, so who can say?
            >
            > Perhaps you think a single line would be "better"?
            >
            > datetime.date(* time.strptime(d , '%Y-%m-%d')[:3])[/color]


            :-) It's not my day I think

            Better (in this case) =

            + Being "pythonic"

            + Avoiding going through a second representation (the tuple)
            if there is some way to create a date object directly.

            jem

            Comment

            • Peter Hansen

              #7
              Re: parsing a date

              Kalle Anke wrote:[color=blue]
              > Better (in this case) =
              >
              > + Being "pythonic"
              >
              > + Avoiding going through a second representation (the tuple)
              > if there is some way to create a date object directly.[/color]

              I think the plainest and simplest approach would be to create a
              well-named function which does the necessary steps (_not_ in a single
              line of code but with nice readable temporary variables) and call that
              whenever you need it.

              Simple utility functions that call a few standard library functions to
              do a job like this are very "pythonic", and trying to avoid a second
              representation when (as in this case) there's no obvious way to do it is
              not pythonic. :-)

              I'd say if you've checked datetime.date() and not found a factory
              function or whatever which knows how to parse a string (as many of us
              have), and if you don't want to download, install, and depend on a
              third-party extension such as mxDateTime which do this already, then a
              function to wrap the ugly details is a very nice way to proceed.

              If you need to do this often, then you've got the beginnings of your own
              utility module or package, and having those around is pretty "pythonic" too.

              (An alternative would be to contribute a patch to add something like
              this to the datetime module. I don't know if it would be accepted, but
              at least it would be the true arbiters of "pythonicis m" who would be
              judging the matter, not you or I. :-) )

              -Peter

              Comment

              Working...