2

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()
Arnold Hotz
  • 179
  • 1
  • 3
  • 8

2 Answers2

2

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
McGrady
  • 10,869
  • 13
  • 47
  • 69
  • Thanks, how do i actually edit the file so that it now contains the line 1, '1/10/2001', '2/28/2004', 100, 'HELEN', 'PRICE' – Arnold Hotz Apr 17 '17 at 05:05
  • @ArnoldHotz you can append it to a list and write the list to a file. Have a look at this [question](http://stackoverflow.com/questions/899103/writing-a-list-to-a-file-with-python) and have a try. – McGrady Apr 17 '17 at 05:16
0

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)
Xiao Tan
  • 376
  • 3
  • 12