Bug in strptime?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henk-Jan de Jong

    Bug in strptime?

    Hello all,

    I'm completely new in Python and have the next problem. I have a huge file
    with dates a want to convert from one format to another.

    e.g 31-12-2003 should become 31122003. I'm using the next piece of code:


    def changeDate(inDa te, inFormat, outFormat):
    return strftime(outFor mat, strptime(inDate , inFormat))

    #the next part inside the loop reading the file:
    .. .
    newDate = changeDate(oldD ate, "%d-%m-%Y", "%d%m%Y")
    .. .

    When the file is small (less then 50 lines or so), it works fine. If the
    file gets bigger I get the next error:

    File "C:\Python23\li b\_strptime.py" , line 424, in strptime
    raise ValueError("tim e data did not match format: data=%s fmt=%s" %
    ValueError: time data did not match format: data= fmt=%d-%m-%Y

    What can be wrong?? The data seems ok.

    Greetinx,

    Matuka


  • John Roth

    #2
    Re: Bug in strptime?


    "Henk-Jan de Jong" <henkjan@xs4all .nl> wrote in message
    news:3f9c54bc$0 $58714$e4fe514c @news.xs4all.nl ...[color=blue]
    > Hello all,
    >
    > I'm completely new in Python and have the next problem. I have a huge file
    > with dates a want to convert from one format to another.
    >
    > e.g 31-12-2003 should become 31122003. I'm using the next piece of code:
    >
    >
    > def changeDate(inDa te, inFormat, outFormat):
    > return strftime(outFor mat, strptime(inDate , inFormat))
    >
    > #the next part inside the loop reading the file:
    > . .
    > newDate = changeDate(oldD ate, "%d-%m-%Y", "%d%m%Y")
    > . .
    >
    > When the file is small (less then 50 lines or so), it works fine. If the
    > file gets bigger I get the next error:
    >
    > File "C:\Python23\li b\_strptime.py" , line 424, in strptime
    > raise ValueError("tim e data did not match format: data=%s fmt=%s" %
    > ValueError: time data did not match format: data= fmt=%d-%m-%Y
    >
    > What can be wrong?? The data seems ok.[/color]

    Put a try block around the line causing the error, and print out the
    input line at that point.

    There is a high probability that enlightenment will occur...

    John Roth[color=blue]
    >
    > Greetinx,
    >
    > Matuka
    >
    >[/color]


    Comment

    • Henk-Jan de Jong

      #3
      Re: Bug in strptime?

      John,

      good advice! I figured out it's not strptime but some feature in the
      linesplitter I wrote :(

      Thanx

      Matuka

      "John Roth" <newsgroups@jhr othjr.com> wrote in message
      news:vpolq11905 p946@news.super news.com...[color=blue]
      >
      > "Henk-Jan de Jong" <henkjan@xs4all .nl> wrote in message
      > news:3f9c54bc$0 $58714$e4fe514c @news.xs4all.nl ...[color=green]
      > > Hello all,
      > >
      > > I'm completely new in Python and have the next problem. I have a huge[/color][/color]
      file[color=blue][color=green]
      > > with dates a want to convert from one format to another.
      > >
      > > e.g 31-12-2003 should become 31122003. I'm using the next piece of code:
      > >
      > >
      > > def changeDate(inDa te, inFormat, outFormat):
      > > return strftime(outFor mat, strptime(inDate , inFormat))
      > >
      > > #the next part inside the loop reading the file:
      > > . .
      > > newDate = changeDate(oldD ate, "%d-%m-%Y", "%d%m%Y")
      > > . .
      > >
      > > When the file is small (less then 50 lines or so), it works fine. If the
      > > file gets bigger I get the next error:
      > >
      > > File "C:\Python23\li b\_strptime.py" , line 424, in strptime
      > > raise ValueError("tim e data did not match format: data=%s fmt=%s"[/color][/color]
      %[color=blue][color=green]
      > > ValueError: time data did not match format: data= fmt=%d-%m-%Y
      > >
      > > What can be wrong?? The data seems ok.[/color]
      >
      > Put a try block around the line causing the error, and print out the
      > input line at that point.
      >
      > There is a high probability that enlightenment will occur...
      >
      > John Roth[color=green]
      > >
      > > Greetinx,
      > >
      > > Matuka
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Tim Roberts

        #4
        Re: Bug in strptime?

        "Henk-Jan de Jong" <henkjan@xs4all .nl> wrote:[color=blue]
        >
        >I'm completely new in Python and have the next problem. I have a huge file
        >with dates a want to convert from one format to another.
        >
        >e.g 31-12-2003 should become 31122003. I'm using the next piece of code:
        >
        >
        >def changeDate(inDa te, inFormat, outFormat):
        > return strftime(outFor mat, strptime(inDate , inFormat))
        >
        >#the next part inside the loop reading the file:
        >. .
        >newDate = changeDate(oldD ate, "%d-%m-%Y", "%d%m%Y")
        >. .[/color]

        It probably won't help you know, but this is certainly not the way I would
        have solved this problem. There isn't any particular reason why you should
        go to the trouble to treat these as dates, since you aren't really using
        the values. Thus, this would be a LOT quicker:

        newDate = oldDate.replace ('-','')
        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • Henk-Jan de Jong

          #5
          Re: Bug in strptime?

          Hello Tim,

          I'm trying to make a generic routine for date-conversions. In my example the
          format changes from dd-mm-yyyy to ddmmyyyy. I'm doing lots of
          data-conversions, so a next time I maybe want to convert ddmmyyyy to
          yyyymmdd or whatever. Furtermore the conversion-rules are described in an
          xml-file for automatic processing:

          eg. xml-description of the field in infile:
          ..
          ..
          <field format="%d-%m-%Y"></field>
          ..
          ..
          xml-description of the field in oufile:
          ..
          ..
          <field format="%Y%m%d" ></field>
          ..
          ..

          If there is less complex way to get it done, I'd be interested to see how. I
          bought my first Python book last weekend, so I've a lot to learn :))

          Greetinx

          Matuka

          "Tim Roberts" <timr@probo.com > wrote in message
          news:t8pupvk00t th0dat7b5oos7l7 kvj59r44n@4ax.c om...[color=blue]
          > "Henk-Jan de Jong" <henkjan@xs4all .nl> wrote:[color=green]
          > >
          > >I'm completely new in Python and have the next problem. I have a huge[/color][/color]
          file[color=blue][color=green]
          > >with dates a want to convert from one format to another.
          > >
          > >e.g 31-12-2003 should become 31122003. I'm using the next piece of code:
          > >
          > >
          > >def changeDate(inDa te, inFormat, outFormat):
          > > return strftime(outFor mat, strptime(inDate , inFormat))
          > >
          > >#the next part inside the loop reading the file:
          > >. .
          > >newDate = changeDate(oldD ate, "%d-%m-%Y", "%d%m%Y")
          > >. .[/color]
          >
          > It probably won't help you know, but this is certainly not the way I would
          > have solved this problem. There isn't any particular reason why you[/color]
          should[color=blue]
          > go to the trouble to treat these as dates, since you aren't really using
          > the values. Thus, this would be a LOT quicker:
          >
          > newDate = oldDate.replace ('-','')
          > --
          > - Tim Roberts, timr@probo.com
          > Providenza & Boekelheide, Inc.[/color]


          Comment

          Working...