在OSX上使用格式语言的德语数字分隔符?

pem*_*ahl 12 python macos formatting osx-mountain-lion

更新:答案显示到目前为止它似乎是OSX上与平台相关的错误,它与特定的区域设置有关,因为它们不完全支持分组编号.

更新2:我刚刚在Python的bug跟踪器上打开了一个问题.让我们看看是否有解决此问题的方法.


我想根据德语编号约定格式化整数和浮点数.这可以使用格式语言和表示类型,n但在我的平台上失败.

  • 平台:OS X 10.8.2(Mountain Lion)
  • Python:2.7.3 64位 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

例子:

  • 1234 => 1.234
  • 1234.56 => 1.234,56
  • 1000000 => 1.000.000

到目前为止我尝试了什么:

  1. 设置德语区域设置

    import locale
    locale.setlocale(locale.LC_ALL, 'de_DE')
    
    Run Code Online (Sandbox Code Playgroud)
  2. 格式规范选项,仅识别英语格式.

    '{:,}'.format(1234)
    '1,234'
    
    '{:,}'.format(1234.56)
    '1,234.56'
    
    '{:,}'.format(1000000)
    '1,000,000'
    
    Run Code Online (Sandbox Code Playgroud)
  3. 根据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)
  4. 由@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)

您是否只使用格式语言为我提供解决方案?有没有办法欺骗我的平台上的区域设置接受分组?

Jon*_*ric 9

超级难看,但技术上回答了这个问题:

PEP 378:

'{:,}'.format(1234.56).replace(",", "X").replace(".", ",").replace("X", ".")
'1.234,56'
Run Code Online (Sandbox Code Playgroud)

  • 哇,那......太可怕了.令人惊讶的是,格式的东西是如此灵活,似乎是一个奇怪的限制 (3认同)

Fre*_*son 5

当与德语语言环境一起使用时,这对我有用:

>>> 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)

  • 这是哪个平台的? (2认同)
  • 有趣的是,在 OS X 上,2.7.3 和 3.3.0 都不为我做这件事,我和提问者一样。 (2认同)

Jon*_*ric 5

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)