3

I have a file which contains a list of entries, with dates that contain "Jan","Feb","Mar" etc.

I want

569207,ngph,RUN,512,16,2012-Jan-23 01:42,2012-04-23 10:53

to become

569207,ngph,RUN,512,16,2012-01-23 01:42,2012-04-23 10:53.

I've written the following:

   for line in f:
        if re.search(r'Jan', line):
            f1.write(line.replace('Jan', '01'))
        elif re.search(r'Feb', line):
            f1.write(line.replace('Feb', '02'))
        elif re.search(r'Mar', line):
            f1.write(line.replace('Mar', '03'))
        elif re.search(r'Apr', line):
            f1.write(line.replace('Apr', '04'))
        elif re.search(r'May', line):
            f1.write(line.replace('May', '05'))
        elif re.search(r'Jun', line):
            f1.write(line.replace('Jun', '06'))
        elif re.search(r'Jul', line):
            f1.write(line.replace('Jul', '07'))
        elif re.search(r'Aug', line):
            f1.write(line.replace('Aug', '08'))
        elif re.search(r'Sep', line):
            f1.write(line.replace('Sep', '09'))
        elif re.search(r'Oct', line):
            f1.write(line.replace('Oct', '10'))
        elif re.search(r'Nov', line):
            f1.write(line.replace('Nov', '11'))
        elif re.search(r'Dec', line):
            f1.write(line.replace('Dec', '12'))

which works, but is there a cleaner method? I want it to iterate through an entire file of about 100 lines and automatically change the month to a number. Thanks.

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
ashleh
  • 225
  • 1
  • 4
  • 11
  • 2
    check this out: http://stackoverflow.com/questions/301528/how-to-convert-a-date-in-python – Shep Apr 23 '12 at 10:09
  • Should have mentioned, I want it to iterate through an entire file of about 100 lines and automatically change the month to a number. – ashleh Apr 23 '12 at 10:14
  • right, but that's a different question, no? It's probably better to keep your SO questions about date manipulation and your questions about file IO separate, it leads to more flexible solutions. – Shep Apr 23 '12 at 10:44

3 Answers3

11

Two possibilities:

  1. the datetime module
  2. DIY solution like you have

The python documentation is worth reading, you may find an easier way to do more than just answer this question.

As for the DIY solution it seems like you have a lot of repeat code, and there's no need for a regex. You could just do

for i, month in enumerate(months,1): 
    line.replace(month, str(i))

where months is a list of the months.

Shep
  • 7,990
  • 8
  • 49
  • 71
11

datetime is the answer, for example:

>>> import datetime
>>> datetime.datetime.strptime('Mar 2013',"%b %Y")
datetime.datetime(2013, 3, 1, 0, 0)

Check the format directives at http://docs.python.org/2/library/datetime.html

Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
runseb
  • 111
  • 1
  • 2
2

Also you can use dictionary:

month_numbers = {'-Jan-': '-01-', '-Feb-': '-02-', ...}
for k, v in month_numbers.items():
    line = line.replace(k, v)
print(line)

You use regexpes where you can use simple string replace() method without first checking if month name is in the string. I don't know if string Jan can be in other part of the input string, but IMO it is safer to replace -Jan- to -01-.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114