我试图了解选项值 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.
我尝试了以下代码:
print "This is a large number with formatting applied: {0:n}".format(1384309238430)
我得到输出:
This is a large number with formatting applied: 1384309238430
也就是说,不存在数字分隔符。如何找到我的语言环境设置?如何获取数字分隔符(我认为数字分隔符指的是千位分隔符之类的东西)。
请您参考如下方法:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
print('{0:n}'.format(1234))
产量
1,234
您可以使用 locale.getlocale() 找到您当前的语言环境:
In [31]: locale.getlocale()
Out[31]: ('en_US', 'UTF8')
默认区域设置为 locale.getdefaultlocale()。
在 *nix 系统上,您可以使用命令 locale -a 获取您的机器可识别的语言环境列表。
