Get date format in Python in Windows

一世执手 提交于 2020-01-23 05:27:14

问题


In a sample code given to me for my homework, this line appears:

date_format = locale.nl_langinfo(locale.D_FMT)

But in Windows that line returns the following error:

File "C:\Users\Shadark\Dropbox\IHM\P3\p3_files\www\cgi-bin\todolist.py", line 11, in <module>
date_format = locale.nl_langinfo(locale.D_FMT)
AttributeError: 'module' object has no attribute 'nl_langinfo'

I've read about using localeconv but I only read about it being used currency or numbers. Any idea on uses for the purpose of my code sample or other kind of function?

Thanks in advance.


回答1:


Your problem is probably the fact that locale.nl_langinfo doesn't appear to be available in Windows Python 2.7.x (I don't see it in my copy of Windows 64-bit Python 2.7.3). Looking at the docs at http://docs.python.org/2.7/library/locale.html#locale.nl_langinfo, they specifically say:

This function is not available on all systems, and the set of possible options might also vary across platforms.

Once you've set the locale up with something along the lines of:

locale.setlocale(locale.LC_ALL, 'english')

Then calls to some_date.strftime() will use correct locale specific formatting and strings. So if you want the date in string format, call some_date.strftime('%x') replace the %x with %X for time or %c for both. The full list of strftime formats are documented here.

>>> d = datetime.datetime.now()
... for loc in ('english', 'german', 'french'):
...     locale.setlocale(locale.LC_ALL, loc)
...     print loc, d.strftime('%c -- %x -- %X -- %B -- %A')
english 11/15/2012 4:10:56 PM -- 11/15/2012 -- 4:10:56 PM -- November -- Thursday
german 15.11.2012 16:10:56 -- 15.11.2012 -- 16:10:56 -- November -- Donnerstag
french 15/11/2012 16:10:56 -- 15/11/2012 -- 16:10:56 -- novembre -- jeudi
14: 'French_France.1252'



回答2:


Try removing any pre-compiled files from the test directory.

If the problem persists, try reinstalling your compile extension. Maybe there was a problem with the installation



来源:https://stackoverflow.com/questions/13401587/get-date-format-in-python-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!