您需要调用locale.str 方法,而不是普通的str 构造函数。
import locale
locale.setlocale(locale.LC_NUMERIC,"ru_RU.utf8")
print locale.localeconv()
fv = 2.5
print locale.str(fv)
输出
{'mon_decimal_point': '', 'int_frac_digits': 127, 'p_sep_by_space': 127, 'frac_digits': 127, 'thousands_sep': '\xc2\xa0', 'n_sign_posn': 127, 'decimal_point': ',', 'int_curr_symbol': '', 'n_cs_precedes': 127, 'p_sign_posn': 127, 'mon_thousands_sep': '', 'negative_sign': '', 'currency_symbol': '', 'n_sep_by_space': 127, 'mon_grouping': [], 'p_cs_precedes': 127, 'positive_sign': '', 'grouping': [3, 3, 0]}
2,5
下面是一些代码,演示了一个简单的区域感知打印功能。正如我在评论中提到的,通常最好在打印时提供明确的格式规范。当然,当你不需要花哨的输出时,基本的print a, b, c 表单很方便,例如在非常简单的脚本和开发/调试期间,但是当你将它用于除了最简单的情况之外的任何情况时,这样的输出往往看起来很草率。
简单的format_string % tuple_of_values 样式格式不支持区域设置。 locale 模块确实提供了几个使用旧的% 格式化协议的函数(format 和format_string)。然而,% 样式格式在现代 Python 中被逐步淘汰,取而代之的是内置 format 函数和 str.format 支持的新格式格式。那些format 函数为数字提供本地感知格式类型说明符:n;下面的代码说明了它的用法。
# -*- coding: utf-8 -*-
import locale
locale.setlocale(locale.LC_NUMERIC, "ru_RU.utf8")
def lprint(*args):
lstr = locale.str
print ' '.join([lstr(u) if isinstance(u, float) else str(u) for u in args])
lprint(1.25, 987654, 42.0, 2.33, u"Росси́я".encode('UTF-8'), 3.456)
print locale.format_string('%.2f %d %.3f %.3f %s %f', (1.25, 987654, 42.0, 2.33, u"Росси́я", 3.456))
print '{0:n} {1:n} {2:n} {3:n} {4:n} {5}'.format(1.25, 987654, 42.0, 2.33, 3.456, u"Росси́я".encode('UTF-8'))
输出(在设置为使用 UTF-8 编码的终端中)
1,25 987654 42 2,33 Россия 3,456
1,25 987654 42,000 2,330 Россия 3,456000
1,25 987 654 42 2,33 3,456 Россия
请注意,在第二行中,我们可以传递一个 Unicode 字符串对象,因为locale.format_string 知道编码。在输出的最后一行 987654 打印有千位分隔符,在俄语语言环境中是一个空格。
如果您使用 Python 2.7(或更高版本),则可以将 '{0:n} {1:n} {2:n} {3:n} {4:n} {5}' 格式字符串简化为 '{:n} {:n} {:n} {:n} {:n} {}'。当然,在 Python 3 中,print 语句不再可用:它已被 print 函数取代;在 Python 2 的更高版本中,您可以通过在任何其他 import 语句之前执行 from __future__ import print_function 来访问该函数。