Arnold Hotz
Arnold Hotz

Reputation: 189

How to detect and edit date in file

I have a file that consists of a bunch of lines that have dates in them, for example:

1, '01-JAN-10', '04-JAN-10', 100, 'HELEN', 'PRICE'
2, 'MARK', 'TYER', '05-JAN-10', '06-JAN-10', 120

I want to change the date parts of the lines to a different format, but I don't know how to detect which part of the line has the date fields and I don't know how to replace them with the new date format. I already have a function called changeDate(date) that returns a correctly formatted date given a bad format date. This is my code so far:

def editFile(filename)
    f = open(filename)

    while line:
        line = f.readline()
        for word in line.split():
            #detect if it is a date, and change to new format
    f.close()

Upvotes: 2

Views: 90

Answers (2)

Xiao Tan
Xiao Tan

Reputation: 442

You can use regex to detect. It's hard to modify the file in place, maybe you could write all the new contents to a new file.

import re
with open('filename', 'r') as f:
   input_file = f.read()
# input_file = "1, '01-JAN-10', '04-JAN-10', 100, 'HELEN', 'PRICE'"
dates = re.findall(r'\d+-[A-Za-z]+-\d+', input_file) # output: ['01-JAN-10', '04-JAN-10']
for old in dates:
    input_file.replace(old, changeDate(old)) # your changeDate(date) in your question
with open('new_file', 'w+') as f:
    f.write(input_file)

Upvotes: 0

McGrady
McGrady

Reputation: 11487

You can use strptime and try/catch to do this:

strptime

Return a datetime corresponding to date_string, parsed according to format.

See more details from strftime() and strptime() Behavior.

from datetime import datetime

s="1, '01-JAN-10', '04-FEB-28', 100, 'HELEN', 'PRICE'"
for word in s.replace(' ','').replace('\'','').split(','):
    try:
        dt=datetime.strptime(word,'%y-%b-%d')
        print('{0}/{1}/{2}'.format(dt.month, dt.day, dt.year))
    except Exception as e:
        print(word)

Result:

1
1/10/2001
2/28/2004
100
HELEN
PRICE

Upvotes: 2

Related Questions