By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,275 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,275 IT Pros & Developers. It's quick & easy.

dealing with datetime while parsing a csv

P: 58
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:second

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?
Expand|Select|Wrap|Line Numbers
  1. from datatime import datetime
  2. TmpArr = []
  3. reader = open("c:/test.csv",'r')
  4. for line in reader:
  5.     TmpArr = line.split(',')
  6.     #now TmpArr[1] contains the datatime data
  7.     #i've tried this
  8.     dt = datetime(TmpArr[1])
  9.     #but get a needs an integer error
  10.     #i've tried this too
  11.     dt = datetime.strtime(TmpArr[1],"%Y-%M-%D %h:%m:%s")
  12.     # then i get a needs datetime object
  13.  
lost and would greatly appreciate anyones help with this... Using Python 2.6.3
Thanks ahead of time.
Cheers,
Eric
Sep 10 '10 #1

✓ answered by bvdet

You have to go to the time module to get what you need.
Expand|Select|Wrap|Line Numbers
  1. import datetime, time
  2.  
  3. s = "2010-01-15 23:15:30"
  4. print repr(datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]))
Output:
>>> datetime.datetime(2010, 1, 15, 23, 15, 30)

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You have to go to the time module to get what you need.
Expand|Select|Wrap|Line Numbers
  1. import datetime, time
  2.  
  3. s = "2010-01-15 23:15:30"
  4. print repr(datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]))
Output:
>>> datetime.datetime(2010, 1, 15, 23, 15, 30)
Sep 10 '10 #2

P: 58
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.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])

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

bvdet
Expert Mod 2.5K+
P: 2,851
time.strptime returns a struct_time object with more attributes than datetime.datetime can handle, therefore the slice ([:6]). The asterisk expands the remaining tuple into the individual tuple elements. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")
  2. (2010, 1, 15, 23, 15, 30, 4, 15, -1)
  3. >>> time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6]
  4. (2010, 1, 15, 23, 15, 30)
  5. >>> datetime.datetime(time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
  6. Traceback (most recent call last):
  7.   File "<interactive input>", line 1, in ?
  8. TypeError: function takes at least 3 arguments (1 given)
  9. >>> datetime.datetime(*time.strptime(s, "%Y-%m-%d %H:%M:%S")[:6])
  10. datetime.datetime(2010, 1, 15, 23, 15, 30)
  11. >>> 
Sep 10 '10 #4

P: 58
Thanks, that does make more sense now!
Sep 10 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.