Read time and date from a text file

H

huisky

Hi,

As a newbie, I posted my question here again.
say i have two dics read from a text file by 'split'.

defaultdict(<type 'int'>, {15424: ['Dec', '6', '18:57:40'], 552:
['Dec', '7', '09:31:00'], 15500: ['Dec', '6', '20:17:02'], 18863:
['Dec', '7', '13:14:47'], 18291: ['Dec', '6', '21:01:17'], 18969:
['Dec', '7', '14:28:42'], 18937: ['Dec', '7', '14:21:34']})
defaultdict(<type 'int'>, {15424: ['Dec', '6', '19:42:55'], 18291:
['Dec', '6', '21:01:28'], 15500: ['Dec', '6', '20:26:03'], 18863:
['Dec', '7', '13:24:07']})

and I need to calculate the difference time if the key value is the
same in both dics.

Someone suggested me to use the module 'datetime', but I'm still
wondering how to make it work.
I mean how to assign ['Dec','6','21:01:17'] to a 'datetime' object and
then do the datetime operation.
time=datetime.datetime(cstart[18291]) does NOT work.

thanks in advance
Huisky
 
T

Tim Williams

Hi,

As a newbie, I posted my question here again.
say i have two dics read from a text file by 'split'.

defaultdict(<type 'int'>, {15424: ['Dec', '6', '18:57:40'], 552:
['Dec', '7', '09:31:00'], 15500: ['Dec', '6', '20:17:02'], 18863:
['Dec', '7', '13:14:47'], 18291: ['Dec', '6', '21:01:17'], 18969:
['Dec', '7', '14:28:42'], 18937: ['Dec', '7', '14:21:34']})

defaultdict(<type 'int'>, {15424: ['Dec', '6', '19:42:55'], 18291:
['Dec', '6', '21:01:28'], 15500: ['Dec', '6', '20:26:03'], 18863:
['Dec', '7', '13:24:07']})

and I need to calculate the difference time if the key value is the
same in both dics.

Someone suggested me to use the module 'datetime', but I'm still
wondering how to make it work.
I mean how to assign ['Dec','6','21:01:17'] to a 'datetime' object and
then do the datetime operation.
time=datetime.datetime(cstart[18291])       does NOT work.

thanks in advance
Huisky

You can use datetime.datetime.strptime() to create a datetime object
from a string representing a date

datetime.datetime(1900, 12, 7, 13, 24, 7)

Of course, you need to put in the correct year.

datetime.strptime(date_string, format)
Return a datetime corresponding to date_string, parsed according to
format. This is equivalent to datetime(*(time.strptime(date_string,
format)[0:6])). ValueError is raised if the date_string and format
can’t be parsed by time.strptime() or if it returns a value which
isn’t a time tuple.
 
P

Peter Otten

huisky said:
As a newbie, I posted my question here again.
say i have two dics read from a text file by 'split'.

Please don't start a new thread when you are still asking about the same
topic.
defaultdict(<type 'int'>, {15424: ['Dec', '6', '18:57:40'], 552:
['Dec', '7', '09:31:00'], 15500: ['Dec', '6', '20:17:02'], 18863:
['Dec', '7', '13:14:47'], 18291: ['Dec', '6', '21:01:17'], 18969:
['Dec', '7', '14:28:42'], 18937: ['Dec', '7', '14:21:34']})
defaultdict(<type 'int'>, {15424: ['Dec', '6', '19:42:55'], 18291:
['Dec', '6', '21:01:28'], 15500: ['Dec', '6', '20:26:03'], 18863:
['Dec', '7', '13:24:07']})

I think you should use a normal dict. A default value of 0 doesn't make much
sense here.
and I need to calculate the difference time if the key value is the
same in both dics.

Someone suggested me to use the module 'datetime', but I'm still
wondering how to make it work.
I mean how to assign ['Dec','6','21:01:17'] to a 'datetime' object and
then do the datetime operation.
time=datetime.datetime(cstart[18291]) does NOT work.

Chris Rebert also suggested that you use the strptime() method. To spell it
out a bit:
s = " ".join(cstart[18291])
s 'Dec 6 21:01:17'
datetime.datetime.strptime(s, "%b %d %H:%M:%S")
datetime.datetime(1900, 12, 6, 21, 1, 17)

You can learn about the format codes here:

http://docs.python.org/library/time.html#time.strftime

Note that strptime() assumes 1900 as the year which may lead to errors in
leapyears and when start and completion time are in different years.

Peter
 
H

huisky

huisky said:
As a newbie, I posted my question here again.
say i have two dics read from a text file by 'split'.

Please don't start a new thread when you are still asking about the same
topic.
defaultdict(<type 'int'>, {15424: ['Dec', '6', '18:57:40'], 552:
['Dec', '7', '09:31:00'], 15500: ['Dec', '6', '20:17:02'], 18863:
['Dec', '7', '13:14:47'], 18291: ['Dec', '6', '21:01:17'], 18969:
['Dec', '7', '14:28:42'], 18937: ['Dec', '7', '14:21:34']})
defaultdict(<type 'int'>, {15424: ['Dec', '6', '19:42:55'], 18291:
['Dec', '6', '21:01:28'], 15500: ['Dec', '6', '20:26:03'], 18863:
['Dec', '7', '13:24:07']})

I think you should use a normal dict. A default value of 0 doesn't make much
sense here.
and I need to calculate the difference time if the key value is the
same in both dics.
Someone suggested me to use the module 'datetime', but I'm still
wondering how to make it work.
I mean how to assign ['Dec','6','21:01:17'] to a 'datetime' object and
then do the datetime operation.
time=datetime.datetime(cstart[18291])       does NOT work.

Chris Rebert also suggested that you use the strptime() method. To spell it
out a bit:
s = " ".join(cstart[18291])
s 'Dec 6 21:01:17'
datetime.datetime.strptime(s, "%b %d %H:%M:%S")

datetime.datetime(1900, 12, 6, 21, 1, 17)

You can learn about the format codes here:

http://docs.python.org/library/time.html#time.strftime

Note that strptime() assumes 1900 as the year which may lead to errors in
leapyears and when start and completion time are in different years.

Peter

Thanks a lot, Peter.
It helps a lot!
I see the problem of year. But the question is the source file does
NOT provide the year information.
for instance if i have one record as ['Dec','6','21:01:17'], and the
other as ['Jan','6','21:01:17']
these two records may be in different year. Will it be a problem to
use the 'datetime' do the time difference calculation?

regards

Huisky

Huisky
 
P

Peter Otten

huisky said:
I see the problem of year. But the question is the source file does
NOT provide the year information.
for instance if i have one record as ['Dec','6','21:01:17'], and the
other as ['Jan','6','21:01:17']
these two records may be in different year. Will it be a problem to
use the 'datetime' do the time difference calculation?

You have a problem that is independent of Python. You have to guess the
correct year to get the correct time interval. One approach would be to use
the year from the file's timestamp, then calculate the differences, and
whenever you get a negative interval add one year to the end date.

This will still result in rare ValueErrors (Feb 29 in non-leapyears) and
some silent miscalculations; I fear you'll have to live with that.

Peter
 
H

huisky

huisky said:
I see the problem of year. But the question is the source file does
NOT provide the year information.
for instance if i have one record as ['Dec','6','21:01:17'], and the
other as ['Jan','6','21:01:17']
these two records may be in different year. Will it be a problem to
use the 'datetime' do the time difference calculation?

You have a problem that is independent of Python. You have to guess the
correct year to get the correct time interval. One approach would be to use
the year from the file's timestamp, then calculate the differences, and
whenever you get a negative interval add one year to the end date.

This will still result in rare ValueErrors (Feb 29 in non-leapyears) and
some silent miscalculations; I fear you'll have to live with that.

Peter

I see, what i'm doing is only for personal usage. should be no problem
at all.
thanks a lot, Peter!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top