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

Character string wind up as a datetime.date object?

P: 3
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.
Mar 14 '10 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 391
Hi

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

Expand|Select|Wrap|Line Numbers
  1. In [1]: import datetime
  2.  
  3. In [2]: st="2010.3.13, 0.0.0, 10.1, 12.2, 9.9, 10.1, 2500"
  4.  
  5. In [3]: d,t,o,h,l,c,v = st.split(',')
  6.  
  7. In [4]: d
  8. Out[4]: '2010.3.13'
  9.  
  10. In [5]: type(d)
  11. Out[5]: <type 'str'>
  12.  
Mar 14 '10 #2

Expert 100+
P: 391
Hi.

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

Expand|Select|Wrap|Line Numbers
  1. In [1]: import datetime
  2.  
  3. In [2]: st="2010.3.13, 0.0.0, 10.1, 12.2, 9.9, 10.1, 2500"
  4.  
  5. In [3]: d,t,o,h,l,c,v = st.split(',')
  6.  
  7. In [4]: d
  8. Out[4]: '2010.3.13'
  9.  
  10. In [5]: type(d)
  11. Out[5]: <type 'str'>
  12.  
Mar 14 '10 #3

P: 3
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:
Expand|Select|Wrap|Line Numbers
  1. for lines in f:
  2.         d,t,o,h,l,c,v = lines.split(',')
  3.         yr,mo,day = d.split('.')
  4.         if datetime.date(string.atoi(yr),string.atoi(mo),string.atoi(day)).weekday() < 5:   #discard Saturday and Sunday
  5.                 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!
Mar 14 '10 #4

Expert 100+
P: 391
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
Mar 15 '10 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Please use code tags when posting code. See Posting Guidelines here.
Mar 15 '10 #6

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. data = "2010.3.13, 14.45.7, 10.1, 12.2, 9.9, 10.1, 2500"
  3.  
  4. d,t,o,h,l,c,v = [s.strip() for s in data.split(',')]
  5. dateObj = time.strptime(d, "%Y.%m.%d")
  6. print dateObj
  7.  
  8. dateObj1 = time.strptime(".".join([d, t]), "%Y.%m.%d.%H.%M.%S")
  9. print dateObj1
  10.  
  11. print time.strftime("%c", dateObj1)
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> (2010, 3, 13, 0, 0, 0, 5, 72, -1)
  2. (2010, 3, 13, 14, 45, 7, 5, 72, -1)
  3. 03/13/10 14:45:07
For what it's worth, you can convert a struct_time object into a datetime.datetime object:
Expand|Select|Wrap|Line Numbers
  1. >>> import datetime
  2. >>> datetime.datetime(*dateObj1[:6])
  3. datetime.datetime(2010, 3, 13, 14, 45, 7)
  4. >>> 
Mar 15 '10 #7

P: 3
@Glenton
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.
Mar 15 '10 #8

Post your reply

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