Character string wind up as a datetime.date object?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • w41ter
    New Member
    • Mar 2010
    • 3

    Character string wind up as a datetime.date object?

    I'm writing a very simple python program to read in an ascii csv file with multiple records formatted like this:

    "2010.3.13, 0.0.0, 10.1, 12.2, 9.9, 10.1, 2500" # i.e. a list of daily stock prices with date,time,open, high,low,close, volume fields

    I use this python code to read each line from the ascii file named f:

    for line in f: d,t,o,h,l,c,v = line.split(',') # where d stands for date, etc

    Much to my pleasant shock, the ascii string "2010.3.13" ends up as a datetime.date object stored in the variable 'd'.

    So, how did this pleasant surprise happen? Why isn't that string stored in 'd' as the string "2010.3.13" instead of as a date object?

    Python must be sneaking around behind my back doing automagical type conversions, but how/where is it happening?

    Thanks!

    BTW, I did "import datetime" because I was planning to do all the ugly date conversions myself. That's why I'm pleasantly surprised.
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

    I got this, so probably you need to post your code to find the "magic"...

    Code:
    In [1]: import datetime
    
    In [2]: st="2010.3.13, 0.0.0, 10.1, 12.2, 9.9, 10.1, 2500"
    
    In [3]: d,t,o,h,l,c,v = st.split(',')
    
    In [4]: d
    Out[4]: '2010.3.13'
    
    In [5]: type(d)
    Out[5]: <type 'str'>

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      Hi.

      I got this, so you probably need to post your code for us to find your magic!

      Code:
      In [1]: import datetime
      
      In [2]: st="2010.3.13, 0.0.0, 10.1, 12.2, 9.9, 10.1, 2500"
      
      In [3]: d,t,o,h,l,c,v = st.split(',')
      
      In [4]: d
      Out[4]: '2010.3.13'
      
      In [5]: type(d)
      Out[5]: <type 'str'>

      Comment

      • w41ter
        New Member
        • Mar 2010
        • 3

        #4
        Hrrmph. This morning the magic is gone and I get a string in d, just like you did. Last night I was running the code in a live python session and I obviously did something I can't remember now :(

        Today I got the job done with this code instead:
        Code:
        for lines in f:
                d,t,o,h,l,c,v = lines.split(',')
                yr,mo,day = d.split('.')
                if datetime.date(string.atoi(yr),string.atoi(mo),string.atoi(day)).weekday() < 5:   #discard Saturday and Sunday
                        print d,o,h,l,c
        The code works, but it looks ugly to me. I'm hoping that python will have a more elegant way to do it. Any suggestions?

        Thanks!
        Last edited by bvdet; Mar 15 '10, 01:20 AM. Reason: Add code tags

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Where did you come across the atoi function?! It's been deprecated since version 2.0. See docs (I had to look it up). Use int(yr),int(mo) ,int(day) instead.

          Other than that it seems pretty elegant to me! Terse, efficient, clear. What more would you want? ;D

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Please use code tags when posting code. See Posting Guidelines here.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              You could use time.strptime to convert the string into a struct_time object and time.strftime to convert to another string in a different format. In addition, if the time was of any importance, it could easily be included. Example:
              Code:
              import time
              data = "2010.3.13, 14.45.7, 10.1, 12.2, 9.9, 10.1, 2500"
              
              d,t,o,h,l,c,v = [s.strip() for s in data.split(',')]
              dateObj = time.strptime(d, "%Y.%m.%d")
              print dateObj
              
              dateObj1 = time.strptime(".".join([d, t]), "%Y.%m.%d.%H.%M.%S")
              print dateObj1
              
              print time.strftime("%c", dateObj1)
              Output:
              Code:
              >>> (2010, 3, 13, 0, 0, 0, 5, 72, -1)
              (2010, 3, 13, 14, 45, 7, 5, 72, -1)
              03/13/10 14:45:07
              For what it's worth, you can convert a struct_time object into a datetime.dateti me object:
              Code:
              >>> import datetime
              >>> datetime.datetime(*dateObj1[:6])
              datetime.datetime(2010, 3, 13, 14, 45, 7)
              >>>

              Comment

              • w41ter
                New Member
                • Mar 2010
                • 3

                #8
                Originally posted by Glenton
                Where did you come across the atoi function?! It's been deprecated since version 2.0. See docs (I had to look it up). Use int(yr),int(mo) ,int(day) instead.

                Other than that it seems pretty elegant to me! Terse, efficient, clear. What more would you want? ;D
                I think perl is less fussy about automatic type conversions, isn't it? Haven't done much with perl, but I got that impression, anyway.

                Heh, I got atoi from an O'Reilly Python book I bought in 1999 and just got around to reading last week :)

                Thanks to bvdet for the strptime examples, looks useful.

                Comment

                Working...