pem*_*ahl 12 python macos formatting osx-mountain-lion
我想根据德语编号约定格式化整数和浮点数.这可以使用格式语言和表示类型,n但在我的平台上失败.
(v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin例子:
1234 => 1.2341234.56 => 1.234,561000000 => 1.000.000到目前为止我尝试了什么:
设置德语区域设置
import locale
locale.setlocale(locale.LC_ALL, 'de_DE')
Run Code Online (Sandbox Code Playgroud)格式规范选项,仅识别英语格式.
'{:,}'.format(1234)
'1,234'
'{:,}'.format(1234.56)
'1,234.56'
'{:,}'.format(1000000)
'1,000,000'
Run Code Online (Sandbox Code Playgroud)根据Python文档,整数和浮动表示类型n应该做我想要的,但事实并非如此.
'{:n}'.format(1234)
'1234'
'{:n}'.format(1234.56)
'1234,56' # at least the comma was set correctly here
'{:n}'.format(1000000)
'1000000'
'{:n}'.format(12345769.56)
'1,23458e+07' # it's doing weird things for large floats
Run Code Online (Sandbox Code Playgroud)由@JFSebastian启发的更多示例和比较:
for n in [1234, 1234.56, 1000000, 12345769.56]:
print('{0:,} {0:n}'.format(n))
fmt, val = "%d %f", (n, n)
print(fmt % val)
print(locale.format_string(fmt, val))
print(locale.format_string(fmt, val, grouping=True))
print('-'*60)
Run Code Online (Sandbox Code Playgroud)
这会在我的平台上产生以下错误结果:
1,234 1234
1234 1234.000000
1234 1234,000000
1234 1234,000000
------------------------------------------------------------
1,234.56 1234,56
1234 1234.560000
1234 1234,560000
1234 1234,560000
------------------------------------------------------------
1,000,000 1000000
1000000 1000000.000000
1000000 1000000,000000
1000000 1000000,000000
------------------------------------------------------------
12,345,769.56 1,23458e+07
12345769 12345769.560000
12345769 12345769,560000
12345769 12345769,560000
------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
我没有得到的正确结果看起来像这样:
1,234 1.234
1234 1234.000000
1234 1234,000000
1.234 1.234,000000
------------------------------------------------------------
1,234.56 1.234,56
1234 1234.560000
1234 1234,560000
1.234 1.234,560000
------------------------------------------------------------
1,000,000 1.000.000
1000000 1000000.000000
1000000 1000000,000000
1.000.000 1.000.000,000000
------------------------------------------------------------
12,345,769.56 1,23458e+07
12345769 12345769.560000
12345769 12345769,560000
12.345.769 12.345.769,560000
------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)您是否只使用格式语言为我提供解决方案?有没有办法欺骗我的平台上的区域设置接受分组?
超级难看,但技术上回答了这个问题:
从PEP 378:
'{:,}'.format(1234.56).replace(",", "X").replace(".", ",").replace("X", ".")
'1.234,56'
Run Code Online (Sandbox Code Playgroud)
当与德语语言环境一起使用时,这对我有用:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
'de_DE'
>>> '{0:n}'.format(1234.56)
'1.234,56'
Run Code Online (Sandbox Code Playgroud)
这是在 Windows 7 下的 Cygwin 中:
>>> import sys
>>> print sys.version
2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1]
Run Code Online (Sandbox Code Playgroud)
locale不幸的是,Python的模块实现在不同平台上有很大差异.它实际上只是围绕C库供应商的语言环境概念的一个轻量级包装器.
因此,在Windows 7上,使用64位的Python 2.7.3,这恰好可行(注意:locales在Windows中有不同的名称):
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'deu_deu')
'German_Germany.1252'
>>> '{0:n}'.format(1234.56)
'1.234,56'
Run Code Online (Sandbox Code Playgroud)
是否使用千位分隔符可以通过检查"本地约定"来确定:
>>> locale.localeconv()['grouping'] # On Windows, 'deu_deu'.
[3, 0] # Insert separator every three digits.
>>> locale.localeconv()['grouping'] # On OS X, 'de_DE'.
[127] # No separator (locale.CHAR_MAX == 127).
>>> locale.localeconv()['grouping'] # Default C locale.
[] # Also no separator.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2905 次 |
| 最近记录: |