2012-11-15 99 views
6

在給我要我的家庭作業的樣本代碼,出現這一行:獲取日期格式在Python在Windows

date_format = locale.nl_langinfo(locale.D_FMT) 

而在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' 

我已閱讀有關使用localeconv但我只讀了它正在使用的貨幣或數字。任何想法用於我的代碼示例或其他類型的功能?

在此先感謝。

回答

5

您的問題可能是locale.nl_langinfo似乎不可用在Windows Python 2.7.x(我沒有看到它在我的Windows 64位Python 2.7副本中0.3)。望着文檔在http://docs.python.org/2.7/library/locale.html#locale.nl_langinfo,他們具體說了:

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

一旦你沿東西線設置地點了:

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

然後調用some_date.strftime()將使用正確的語言環境特定的格式和字符串。所以如果你想要字符串格式的日期,撥打some_date.strftime('%x')替換%x%X時間或%c兩者。 strftime格式的完整列表記錄在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

還有一些解決方法,用於以類似於nl_langinfo的方式在Python中獲取date_format,但不使用它? – Shadark

+0

如何在Windows上獲取日期/時間/日期時間格式字符串? – Romeno

1

嘗試從測試目錄中刪除任何預編譯的文件。

如果問題仍然存在,請嘗試重新安裝編譯擴展。也許有安裝問題