views:

78

answers:

3
+1  Q: 

Python Split usage

I'm cocking this up and it should be really simple but the value of sortdate is none (note im only doing this because converting a string to a date in Python is a bugger).

DateToPass = str(self.request.get('startdate'))
mybreak.startdate = DateToPass
faf = DateToPass.split('-')
sortdate = str(faf[2] + faf[1] + faf[0])

That should work? but its just being stored as null though the datetopass is being stored fine.

+1  A: 

If the real issue is converting a string to a time, as you have indicated, then have you looked into time.strptime?

Daniel DiPaolo
Its to datetype i was looking to achieve originally; and yep ive been down that rabbit hole; I think adding this extra column for sorting based on YYmmdd will suffice as this is a rush job.
Chris M
+4  A: 

It would be helpful to see what self.request.get('startdate') looked like. Is it ISO (YYYY-MM-DD)? If so I'll show an example using datetime. There's no need for splits because of datetime.datetime.strptime:

>>> import datetime
>>> date_to_pass = '2010-05-07'
>>> sortdate = datetime.datetime.strptime(date_to_pass, '%Y-%m-%d')
>>> sortdate
datetime.datetime(2010, 5, 7, 0, 0)

Datetime objects are sortable, so there's no need to convert to a string. Unless I'm missing the point of your question.

jathanism
Sorry time delay long drive home. The date is being entered in UK format (dd-mm-YY) 21-05-2010; I should probably test it locally a bit now I'm not working from a remote desktop but basically all routes either lead to an error regarding the string when I used '%d-%m-%Y' which started me off on the wild goose chase. I expect I'll try this code now and itll work ;o) heres hoping
Chris M
holy cunt burger; just noticed I havent actually assigned the model on the left of the variable it should be mybreak.sortdate I'll be back in an hour when ive finished banging my head
Chris M
Thanks for the help
Chris M
Glad to help. In that case your format string would be `'%d-%m-%y'` (small y for year, big Y for century + year).
jathanism
Ta, It was a 'hard of thinking' moment, so much so I mooched around http://docs.python.org/library/datetime.html then rewrote the whole app. :o) Pythons not all bad, just niggly.
Chris M
+1  A: 

If your input date is in the format 'YYYY-MM-DD' then the code you have shold work fine. There are a few extraneous str() calls, and yeah, it'd be more proper to use strptime, but nothing that should break.

For example, this works:

Python 2.5.2 (r252:60911, Apr 15 2008, 11:28:25)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> startdate = '2002-04-20'
>>> splitdate = startdate.split('-')
>>> type(splitdate[0])
<type 'str'>
>>> splitdate[2]+splitdate[1]+splitdate[0]
'20042002'

So the two places I'd look are:

  1. What is the format you get from self.request.get('startdate') and store in DateToPass?
  2. You haven't shown us the code where you store sortdate. Is it broken?
Matt Miller
The store is working fine for storing the date as string, but it was storing the reconstituted date (as in the code chunk) as null; all the other data in the model was fine. Hence I assumed split worked differently or I was going totally wrong :o) ... Yours answers the question as it was asked (regarding splits) which is handy to understand as I'll be using it a lot. Thanks, now I can go back to the C++ ;o) >> As below I missed the f#@ing model off the start of the string argh
Chris M
Thanks for the help dude :)
Chris M