473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Localized month names?

How do I get a list of localized month names for current locale? The
first thing that came to my mind was an ugly hack:

import datetime
for i in range(12):
# as I remember, all months in 2005 had 1st in days
datetime.date(2005, i + 1, 1).strftime('%B')

but I am sure there is a better way...

--
Jarek Zgoda
http://jpa.berlios.de/
Mar 13 '06 #1
5 6102
Jarek Zgoda wrote:
How do I get a list of localized month names for current locale? The
first thing that came to my mind was an ugly hack:

import datetime
for i in range(12):
# as I remember, all months in 2005 had 1st in days
datetime.date(2005, i + 1, 1).strftime('%B')


doesn't look very ugly to me...

the strftime tables are hidden deep inside the C library, but I guess you
could use the _strptime implementation module to dig out the information
you're after:
import locale
locale.setlocale(locale.LC_ALL, "sv_SE") 'sv_SE' import _strptime
vars(_strptime.LocaleTime())

{'lang': ('sv_SE', 'ISO8859-1'), 'am_pm': ['', ''], 'f_month': ['', 'januari',
'februari', 'mars', 'april', 'maj', 'juni', 'juli', 'augusti', 'september', 'oktober',
'november', 'december'], 'LC_date': '%Y-%m-%d', 'a_weekday': ['m\xe5n',
'tis', 'ons', 'tor', 'fre', 'l\xf6r', 's\xf6n'], 'f_weekday': ['m\xe5ndag', 'tisdag',
'onsdag', 'torsdag', 'fredag', 'l\xf6rdag', 's\xf6ndag'], 'LC_date_time':
'%a %d %b %Y %H.%M.%S', 'timezone': (frozenset(['utc', 'cet', 'gmt']),
frozenset(['cest'])), 'a_month': ['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun',
'jul', 'aug', 'sep', 'okt', 'nov', 'dec'], 'LC_time': '%H.%M.%S'}

(I'm pretty sure _strptime uses your ugly hack-approach to extract this
information from the C library...)

hope this helps!

</F>

Mar 13 '06 #2
Jarek Zgoda wrote:
How do I get a list of localized month names for current locale? The
first thing that came to my mind was an ugly hack:

import locale
locale.nl_langinfo(locale.MON_1)

'January'

--
Benji York
Mar 13 '06 #3
Benji York schrieb:
Jarek Zgoda wrote:
How do I get a list of localized month names for current locale? The
first thing that came to my mind was an ugly hack:


import locale
locale.nl_langinfo(locale.MON_1)

'January'


What did you leave out? I get

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
locale.nl_langinfo(locale.MON_1)
AttributeError: 'module' object has no attribute 'nl_langinfo'

(Python 2.4, german Windows XP Pro)

Moreover, 'January' is probably not localized.

--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Si*************@Bibliothek.Uni-Augsburg.DE
Mar 14 '06 #4
Sibylle Koczian wrote:
Benji York schrieb:
Jarek Zgoda wrote:
How do I get a list of localized month names for current locale? The
first thing that came to my mind was an ugly hack:
> import locale
> locale.nl_langinfo(locale.MON_1)

'January'


What did you leave out?


Nothing, most likely.
I get

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
locale.nl_langinfo(locale.MON_1)
AttributeError: 'module' object has no attribute 'nl_langinfo'

(Python 2.4, german Windows XP Pro)

Moreover, 'January' is probably not localized.


Python 2.4.2 (#4, Nov 19 2005, 13:25:59)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import locale
locale.nl_langinfo(locale.MON_1) 'January' locale.setlocale(locale.LC_ALL, "") 'de_DE.UTF-8' locale.nl_langinfo(locale.MON_1)

'Januar'
Peter
Mar 14 '06 #5
Peter Otten wrote:
Sibylle Koczian wrote:
What did you leave out?

Nothing, most likely.

I get

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
locale.nl_langinfo(locale.MON_1)
AttributeError: 'module' object has no attribute 'nl_langinfo'

(Python 2.4, german Windows XP Pro)

Moreover, 'January' is probably not localized.

Python 2.4.2 (#4, Nov 19 2005, 13:25:59)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import locale
locale.nl_langinfo(locale.MON_1)


From the docs:
nl_langinfo( option)

Return some locale-specific information as a string. This function
is not available on all systems, and the set of possible options might
also vary across platforms.

It doesn't seem to be available on Windows:
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

In [1]: import locale

In [2]: locale.nl_langinfo(locale.MON_1)
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in ?
AttributeError: 'module' object has no attribute 'nl_langinfo'

Kent
Mar 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jm | last post by:
I am trying to use datepart to get the real name of the month like "April" or "APR" not just "4." I could not find it in the documentation. Sorry. Thank you.
5
by: Ataru Morooka | last post by:
Hi, my table has to have a column with the months names (january, february...). When I order it by month it is ordered alphabetically and that's not what I need. Reading this ng I found someone...
2
by: Rupert Street | last post by:
I would like to set up a command button on a timesheet form to preview the jobs a contractor has worked on for the month. I would like if possible when the button is clicked for it to ask for the...
4
by: M | last post by:
Hello, I have code like dt.DayOfWeek.ToString(CultureInfo.CurrentCulture) where dt is a DateTime object. I get something like "Sunday", "Monday", etc... while I'm expecting it to be in...
2
by: Burghew | last post by:
Thanks for the previous replies..... What I need is to generate invoices for customers whom we rent out equipment. I will be choosing the date and year say for December 2005 print the invoices...
1
by: durumdara | last post by:
Hi ! WXP, Py2.4.3. I want to get localized informations like month names, format parameters, etc. But nl_langinfo is not exists. Have the Python a way to get these informations in...
5
by: soni2926 | last post by:
Hi, I have a web application, asp.net and c# done in 2.0, which is going to return rows from the db with dates or certain events. The db is going to have events dates for the entire year, but on...
0
by: Hvid Hat | last post by:
Hi The following code will give me the month names in the given culture: CultureInfo cultureInfo = new CultureInfo(CultureInfo.CurrentCulture.Name); string months =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.