python python日期时间本地化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2090840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python datetime localization
提问by jd.
What do I need to do (modules to load, locale methods to invoke, etc.) so that when I call:
我需要做什么(要加载的模块、要调用的语言环境方法等),以便在我调用时:
datetime.date(2009,1,16).strftime("%A %Y-%b-%d")
instead of getting:
而不是得到:
Out[20]: 'Friday 2009-Jan-16'
i get spanish/french/german/... output
我得到西班牙语/法语/德语/...输出
Out[20]: 'Viernes 2009-Ene-16'
without having to change my whole operating system's locale (i.e. just use python calls to dynamically set the locale and keep the changes scoped within my app)
无需更改我的整个操作系统的语言环境(即只需使用 python 调用动态设置语言环境并将更改范围保持在我的应用程序内)
Thanks.
谢谢。
采纳答案by Ignacio Vazquez-Abrams
回答by e18r
On Ubuntu,
在 Ubuntu 上,
$> sudo locale-gen es_ES.UTF-8
$> sudo dpkg-reconfigure locales
$> python
>>> import locale
>>> locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')
回答by Kyle Lutz
After setting your locale (with locale.setlocale
) You can use the locale
modules' nl_langinfo
method like so:
设置您的语言环境(使用locale.setlocale
)后,您可以像这样使用locale
模块的nl_langinfo
方法:
time.strftime(locale.nl_langinfo(locale.D_T_FMT), time.localtime())