0

I am trying to get a date formatted, i.e., from Jan to JA, Feb to FE. I have a dictionary built to do this, however my code only works randomly, e.g., sometimes every 4 times, sometimes every 24 times. I am new to Python, and I can't make sense of this. The output from the below code is 16Jun28 most of the time, but correctly 16JN28 sometimes.

import datetime
month_code = {'Jan': 'JA', 
      'Feb': 'FE', 
      'Mar': 'MR', 
      'Apr': 'AL', 
      'May': 'MA', 
      'Jun': 'JN', 
      'Jul': 'JL', 
      'Aug': 'AU', 
      'Sep': 'SE', 
      'Oct': 'OC', 
      'Nov': 'NO', 
      'Dec': 'DE'}

today = datetime.datetime.now()
DD = datetime.timedelta(days=90)    
use_by = today + DD
use_by_str = use_by.strftime("%y-%b-%d")

def label_function():
    month = use_by.strftime("%b")
    year = use_by.strftime("%y")
    day = use_by.strftime("%d")
    return year + month + day

line = label_function()

for k, v in month_code.items():
    Result = line.replace(k, v)
print(Result)
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
Matt S.
  • 1
  • 1
  • 7

2 Answers2

2

You are iterating through your dictionary to replace your 3 char month name to 2 char, but you're always placing the result in a different variable, and not mutating line itself.

What you'd be looking for would be

for k, v in month_code.items():
    line = line.replace(k, v)

As an aside, why not have your label_function return a tuple of your date string, and then you only need to alter 1 value with 1 lookup instead.

def label_function():
    month = use_by.strftime('%b')
    year = use_by.strftime('%y')
    day = use_by.strftime('%d')
    return year, month, day

year, month, day = label_function()
result = year + month_code[month] + day
Christian Witts
  • 11,375
  • 1
  • 33
  • 46
  • Both solutions worked perfectly. The aside answer is the one I will go with! In the grand scheme of things I was not thinking that 12 iterations was that big of a deal...I appreciate the help! – Matt S. Mar 30 '16 at 13:24
  • This might get me thrown out of the forum, but any ideas on how to convert to VBScript? – Matt S. Mar 30 '16 at 13:30
  • That won't get you thrown out of the forum ;) You can take a look at the answer http://stackoverflow.com/questions/9003923/vbscript-how-can-i-format-date there for an idea on where to get started. – Christian Witts Mar 30 '16 at 13:33
1

Christian has already shown a better way to implement what you need. I'll try to explain the bug.

It works randomly because of a logic error in your code combined with the random ordering of dictionaries.

You have two variables, line and Result. Suppose line is '16Feb28'. Your code

  • takes line, tries to replace Jan with JA, and puts the result (unchanged) into Result.
  • Then it takes line, tries to replace Feb with FE, and puts the result (16FE28) into Result.
  • Then it takes line (16Feb28), tries to replace Mar with MR, and puts the result (16Feb28) into Result.
  • and so on. Can you see the problem now?
  • Finally it takes line (16Feb28), tries to replace Dec with DE, and puts the result (16Feb28) into Result.

The reason that it sometimes works is that in reality dictionaries are not iterated in the order in which you write them in the source code, but in a random one, so the matching entry Feb->FE sometimes happens to be the last one.

Why is the order random? See Why is the order in dictionaries and sets arbitrary?

Community
  • 1
  • 1
Martin Vidner
  • 2,307
  • 16
  • 31
  • Thanks for the details. Is there a reasoning behind the dict is randomly ordered? The explanation you provided makes perfect sense otherwise. – Matt S. Mar 30 '16 at 17:39