473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Searching date and and time from a text file

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.

Nov 22 '05 #1
3 1289
ma********@gmail.com wrote:
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

Nov 22 '05 #2
Thanks. It was like 911

Nov 22 '05 #3

ma********@gmail.com wrote:
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.


http://docs.python.org/lib/bltin-file-objects.html

http://docs.python.org/lib/typesseq.html

http://docs.python.org/lib/typesseq-mutable.html

http://www.amk.ca/python/howto/regex/

and possibly

http://docs.python.org/lib/module-datetime.html

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dixie | last post by:
How can I programatically, take some Date/Time fields present in a table in the current database and change their type to text? dixie
7
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database....
16
by: pmatzek | last post by:
I am attempting to automate a process involving importing a .txt file into an existing table, changing a date in each record, then exporting the table, again as a .txt file. One field contains a...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
1
by: morangi | last post by:
<?php session_start(); error_reporting(E_ERROR && ~E_NOTICE && ~E_WARNING); ob_start(); $link = mysql_connect('localhost', 'root', 'test'); if (!$link) { die('Could not connect: ' ....
0
by: Kassimu | last post by:
Hi guys out there, There is this database Iam creating, I have a table with 40 fields among which there are Date/time, Text, Number, Memo and Yes/No fields and I have created the form bound to that...
0
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
Hello group. I've migrated from Win 2003 server to Win 2008 server. I've been banging my head agaist a wall for several days now trying to figure this out. I have the following script that will...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
5
by: id10t error | last post by:
I am makeing a program that will track productivity by every time a button is pushed i write a record to a text file. The text file contain the time in HHMMSS format. The next time they press a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.