Date using input
Tim Chase
python.list at tim.thechases.com
Thu Sep 24 11:03:43 EDT 2009
> Surely getting it tottally mixed up
>
> from datetime import date
> def ObtainDate(params):
> date = raw_input("Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r
> %4.0r%4.0r%4.0r")
> print date.datetime(year-month-day)
By setting "date = raw_input(...)", you mask the datetime.date
object preventing you from using it in the next "print" line.
Additionally, the "-" aren't used to separate parameters...you
want commas. You also haven't split out the year/month/day bits
to pass to the date.datetime() constructor. So immediate
corrections involve:
1) choose a name other than "date" for the value returned from
raw_input()
2) take that resulting value from step #1 and split it up so you
have the constituent parts. This is a wonderful use for tuple
assignment.
3) After splitting parts up, you still have strings, so you need
to convert them to numbers. (for advanced users, I'd use map()
to do the conversion in step #2)
4) once you have the year, month, and day values as integers, you
can pass them to the datetime.date constructor (instead of the
datetime.date.datetime constructor which is a little weird).
> #Check if txt file with same date exists. If yes apphend to results
> to file.
> date.append(datetime
You'd then want to create a file-name to open, based on the date
object. The strftime() method will help you here (see the docs
on the format string). Once you have the filename, you'll want
to open a file with that name, appending to it if it already
exists (see the docs on the file() object)
-tkc
More information about the Python-list
mailing list