Searching date and and time from a text file

M

masudtarek

Hi, I am new in Python programming. Can anybody give me any idea about
how to detect more than one date and time (like 11/11/2005 ,
10-12-2006, 12:30 etc) from a text file and keep them in a list.
 
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

Hi, I am new in Python programming. Can anybody give me any idea about
how to detect more than one date and time (like 11/11/2005 ,
10-12-2006, 12:30 etc) from a text file and keep them in a list.

well first we read the file

src = file("/your/file").read()

then we need some regular expressions to find the date and time

import re

pattern = {
"time" : re.compile(r"\b\d\d:\d\d\b"),
"date1" : re.compile(r"\b\d\d-\d\d-\d\d\d\d\b"),
"date2" : re.compile(r"\b\d\d/\d\d/\d\d\d\d\b")
}

times = pattern["time"].findall(src)
dates1 = pattern["date1"].findall(src)
dates2 = pattern["date2"].findall(src)

note this is not very good solution though
one should check all the items to be sure
hours are between 0 and 23 and minutes in
range 0 .. 59

for time in times:
t = time.split(":")
assert 0 <= int(t[0]) <= 23
assert 0 <= int(t[1]) <= 59


hth, Daniel
ps: all code untested
 
D

davideugenewarren

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top