1

I want to identify if a string contains a date formatted as MM/DD/YY. It can be any date, as long as its in that format. Not sure how I would denote this.

for line in f:    
    if date in line:
        print(line)

Data:

>Unneeded text<

Account: 5

03/29/18      7,885,216.93-    208,557,351.68     200,672,134.75    2.18        12,151.81                                           
03/30/18      7,885,216.93-    208,557,351.68     200,672,134.75    2.18        12,151.81                                           
03/31/18      7,885,216.93-    208,557,351.68     200,672,134.75    2.18        12,151.81                                           
04/01/18      7,885,216.93-    208,557,351.68     200,672,134.75    2.18        12,151.81                                           
04/02/18     10,487,227.15     202,979,865.76     213,467,092.91    2.17        12,867.30                                           
04/03/18     26,149,970.46-    209,222,696.72     183,072,726.26    2.18        11,086.08                                           
04/04/18     13,606,232.52-    217,761,977.25     204,155,744.73    2.18        12,362.77                                           
04/05/18     29,929,731.83-    228,565,335.73     198,635,603.90    2.19        12,083.68                                           
04/06/18     32,832,695.61-    235,134,802.88     202,302,107.27    2.19        12,306.70                                           
04/07/18     32,832,695.61-    235,134,802.88     202,302,107.27    2.19        12,306.70                                           
04/08/18     32,832,695.61-    235,134,802.88     202,302,107.27    2.19        12,306.70                                           
04/09/18     31,908,656.83-    232,249,566.41     200,340,909.58    2.19        12,187.38                                           
04/10/18     20,367,782.42-    229,302,450.95     208,934,668.53    2.19        12,710.17                                           

>Unneeded text<

Account: 6

03/29/18      7,885,216.93-    208,557,351.68     200,672,134.75    2.18        12,151.81

This is one account's worth of data. There are approx 30 accounts I need to iterate through. The issue is that each section of data is separated by some text that is not necessary.

My current script grabs the account # along with yesterday's data:

for line in f:
        if "ACCOUNT: " + Account in line:  
            TAccount = Account     
                for line in f:
                    if rdate.strftime("%m/%d/%y") in line:
                        print(line)  

I was hoping that there would be a way to just find any string that began with a date formatted as MM/DD/YY

FelixSFD
  • 6,052
  • 10
  • 43
  • 117

4 Answers4

4

You can use this regex to recognize this model MM/DD/YYYY :

date_regex = '(0[1-9]|1[12])\/(0[1-9]|1[0-9]|2[0-9]|3[0-1])\/(\d{4})'

regexr example

and you can use this for MM/DD/YY :

date_regex = '(0[1-9]|1[12])\/(0[1-9]|1[0-9]|2[0-9]|3[0-1])\/(\d{2})'

regexr example

MasOOd.KamYab
  • 944
  • 11
  • 25
  • I added the above to my script and implemented it as: for line in f: if date_regex in line print(line) but nothing prints –  Apr 19 '18 at 05:39
2

You can use re module to find data and you consider to validate the date if it is valid using datetime module

for line in f:
    match=re.search(r'(\d+/\d+/\d+)',line)
    _date = match.group(1)
    try:
        datetime.datetime.strptime(_date,"%m/%d/%y")
        #To DO
    except:
        pass  #not valid date
Roushan
  • 4,074
  • 3
  • 21
  • 38
1

This is how I would extract the date elements from the string:

month=int(f[0:2])
day=int(f[3:5])
Year=int(f[6:8])

Checking if these give a valid date might be a solution to your problem.

jwg
  • 5,547
  • 3
  • 43
  • 57
Programmer S
  • 429
  • 7
  • 21
0

Using regex

import re
s = "dssdfsdfsdfsdf 10/01/18 ssdfsfsf dsfsdfsdfsd"
print(re.findall("\d{2}/\d{2}/\d{2}", s))

Output:

['10/01/18']

In your case:

for line in f:
    date = re.search("\d{2}/\d{2}/\d{2}", line):
    if date:
        print(line, date.group())
Rakesh
  • 81,458
  • 17
  • 76
  • 113