python - filling text file with dates -
i file file dates, , have 02 problems:
from datetime import date, datetime, timedelta def perdelta(start, end, delta): curr = start while curr < end: yield curr curr += delta fo = open("dattes.txt","wb") result in perdelta(date(2011, 10, 10), date(2011, 12, 12), timedelta(days=4)): fo.write(result) fo.close()
1- getting error:
traceback (most recent call last): file "c:\test\date.txt", line 12, in fo.write(result) typeerror: must string or buffer, not datetime.date
2-i output date contiguous (without '-' between day,month , year)
use datetime.date.strftime format date.
you may want write newline('\n'
) inbetween if don't want 201110102011101420111018....
.
so,
... result in perdelta(date(2011, 10, 10), date(2011, 12, 12), timedelta(days=4)): fo.write(result.strftime('%y%m%d\n')) ....
alternative
... result in perdelta(date(2011, 10, 10), date(2011, 12, 12), timedelta(days=4)): fo.write('{0:%y%m%d}\n', result) ...
Comments
Post a Comment