Cur*_*arn 1 python string locale
我试图了解标志的n字符串格式中的选项值究竟是什么type.
PEP 3101说(在可用的整数类型部分):
'n' - Number. This is the same as 'd', except that it uses the
current locale setting to insert the appropriate
number separator characters.
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码:
print "This is a large number with formatting applied: {0:n}".format(1384309238430)
Run Code Online (Sandbox Code Playgroud)
我得到输出:
This is a large number with formatting applied: 1384309238430
Run Code Online (Sandbox Code Playgroud)
也就是说,不存在数字分隔符.如何找到我的区域设置?如何获取数字分隔符(我认为通过数字分隔符,它指的是诸如千位分隔符逗号之类的东西).
unu*_*tbu 10
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
print('{0:n}'.format(1234))
Run Code Online (Sandbox Code Playgroud)
产量
1,234
Run Code Online (Sandbox Code Playgroud)
您可以在以下位置找到当前的区域设置locale.getlocale():
In [31]: locale.getlocale()
Out[31]: ('en_US', 'UTF8')
Run Code Online (Sandbox Code Playgroud)
和默认的语言环境locale.getdefaultlocale().
在*nix系统上,您可以使用该命令获取机器知道的语言环境列表locale -a.