dealing with datetime while parsing a csv

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erbrose
    New Member
    • Oct 2006
    • 58

    dealing with datetime while parsing a csv

    Hello all!
    I am parsing a csv file and one of the fields is a date time field that looks something like this
    2010-01-15 23:15:30
    year-month-day hour24:minute:s econd

    as i loop through this csv i am going to need to do some time arithmetic. my question is, how do i turn that field from a string in my list to a datetime object?
    Code:
    from datatime import datetime
    TmpArr = []
    reader = open("c:/test.csv",'r')
    for line in reader:
        TmpArr = line.split(',')
        #now TmpArr[1] contains the datatime data
        #i've tried this
        dt = datetime(TmpArr[1])
        #but get a needs an integer error
        #i've tried this too
        dt = datetime.strtime(TmpArr[1],"%Y-%M-%D %h:%m:%s")
        # then i get a needs datetime object
    lost and would greatly appreciate anyones help with this... Using Python 2.6.3
    Thanks ahead of time.
    Cheers,
    Eric
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You have to go to the time module to get what you need.
    Code:
    import datetime, time
    
    s = "2010-01-15 23:15:30"
    print repr(datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]))
    Output:
    >>> datetime.dateti me(2010, 1, 15, 23, 15, 30)

    Comment

    • erbrose
      New Member
      • Oct 2006
      • 58

      #3
      Thanks so much...
      now seeing as i don't really see something like this in the documentation, could you explain this line a bit more?
      datetime.dateti me(*time.strpti me(s, "%Y-%m-%d %H:%M:%S")[:6])

      I understand calling datetime.dateti me, but why the * before time? and also what exactly is the [:6] pertaining too
      Thanks again for your help!!!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        time.strptime returns a struct_time object with more attributes than datetime.dateti me can handle, therefore the slice ([:6]). The asterisk expands the remaining tuple into the individual tuple elements. Example:
        Code:
        >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")
        (2010, 1, 15, 23, 15, 30, 4, 15, -1)
        >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]
        (2010, 1, 15, 23, 15, 30)
        >>> datetime.datetime(time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
        Traceback (most recent call last):
          File "<interactive input>", line 1, in ?
        TypeError: function takes at least 3 arguments (1 given)
        >>> datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
        datetime.datetime(2010, 1, 15, 23, 15, 30)
        >>>

        Comment

        • erbrose
          New Member
          • Oct 2006
          • 58

          #5
          Thanks, that does make more sense now!

          Comment

          Working...