Reputation: 19642
So I have some code that uses calendar.month_abbr in Python 3 (on Windows). This returns English month names, even though the settings in 'Additional date, time & regional settings' say 'Dutch'. This is fine for me however - I was after the English names, so I never looked into it further, although it's weird now that I think about it.
Now, when I use that exact same code in Python 2(.7), it returns Dutch names. I suppose this is because 'Language for non-Unicode programs' is set to 'Dutch' on my machine (this is in the 'Administrative' tab on the same dialog as mentioned above).
My code might be run on machines with a variety of locales set. What I'm after is a way to force the locale for calendar.month_abbr to en-US. I don't see a way to pass any locale, so I suppose I have to set it globally, then force this list to be regenerated? Unless its elements are generated on the fly? Either way, how do I go about converting a number in the range [1-12] to its three-letter abbreviation in English, regardless of system settings, for Python 2.7?
Upvotes: 1
Views: 4822
Reputation: 602
Does this work for you?
Check your locale settings:
import calendar
print([calendar.month_abbr[i].lower() for i in range(13)])
Then set your locale back to C or English before parsing English month names. You only need to do this for the LC_TIME category:
import locale
locale.setlocale(locale.LC_TIME, 'C')
Upvotes: 3