Sha*_*ark 7 python locale date-format localeconv
在为我的作业提供给我的示例代码中,此行显示为:
date_format = locale.nl_langinfo(locale.D_FMT)
Run Code Online (Sandbox Code Playgroud)
但在Windows中该行返回以下错误:
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'
Run Code Online (Sandbox Code Playgroud)
我读过有关使用localeconv的内容,但我只读到它使用的货币或数字.关于我的代码示例或其他类型函数的用途的任何想法?
提前致谢.
您的问题可能locale.nl_langinfo是在Windows Python 2.7.x 中似乎没有的事实(我在Windows 64位Python 2.7.3的副本中看不到它).查看http://docs.python.org/2.7/library/locale.html#locale.nl_langinfo上的文档,他们具体说:
此功能并非在所有系统上都可用,并且可能的选项集也可能因平台而异.
一旦您使用以下内容设置了区域设置:
locale.setlocale(locale.LC_ALL, 'english')
Run Code Online (Sandbox Code Playgroud)
然后调用some_date.strftime()将使用正确的特定于语言环境的格式和字符串.所以,如果你想在字符串格式的日期,调用some_date.strftime('%x')替换%x用%X的时间或%c两个.这里记录了strftime格式的完整列表.
>>> 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'
Run Code Online (Sandbox Code Playgroud)