开发者

Compute number of dates between two string dates and return an integer

I have a .txt file data-set like this with the date column of interest:

1181206,356007开发者_如何学Go6,2,01/03/2010,46,45,M,F
2754630,2831844,1,03/03/2010,56,50,M,F
3701022,3536017,1,04/03/2010,40,38,M,F
3786132,3776706,2,22/03/2010,54,48,M,F
1430789,3723506,1,04/05/2010,55,43,F,M
2824581,3091019,2,23/06/2010,59,58,M,F
4797641,4766769,1,04/08/2010,53,49,M,F

I want to work out the number of days between each date and 01/03/2010 and replace the date with the days offset {0, 2, 3, 21...} yielding an output like this:

1181206,3560076,2,0,46,45,M,F
2754630,2831844,1,2,56,50,M,F
3701022,3536017,1,3,40,38,M,F
3786132,3776706,2,21,54,48,M,F
1430789,3723506,1,64,55,43,F,M
2824581,3091019,2,114,59,58,M,F
4797641,4766769,1,156,53,49,M,F

I've been trying for ages and its getting really frustrating. I've tried converting to datetime using the datetime.datetime.strptime( '01/03/2010', "%d/%m/%Y").date() method and then subtracting the two dates but it gives me an output of e.g. '3 days, 0:00:00' but I can't seem to get an output of only the number!


The difference between two dates is a timedelta. Any timedelta instance has days attribute that is an integer value you want.


This is fairly simple. Using the code you gave:

date1 = datetime.datetime.strptime('01/03/2010', '%d/%m/%Y').date()
date2 = datetime.datetime.strptime('04/03/2010', '%d/%m/%Y').date()

You get two datetime objects.

(date2-date1)

will give you the time delta. The mistake you're making is to convert that timedelta to a string. timedelta objects have a days attribute. Therefore, you can get the number of days using it:

(date2-date1).days

This generates the desired output.


Using your input (a bit verbose...)

#!/usr/bin/env python

import datetime

with open('input') as fd:
    d_first = datetime.date(2010, 03, 01)
    for line in fd:
        date=line.split(',')[3]
        day, month, year= date.split(r'/')
        d = datetime.date(int(year), int(month), int(day))
        diff=d - d_first
        print diff.days

Gives

0
2
3
21
64
114
156

Have a look at pleac, a lot of date-example there using python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜