簡體   English   中英

使用語言環境在python中格式化

[英]Formatting in python with locale

我正在嘗試格式化一組數字以顯示為xxx,xxx,xxx而不是xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxx

格式化程序在字典中進行迭代,其中的值是一個列表。

看起來是這樣的:

{'ST': [Decimal('21529992.247811'), 
Decimal('75922363.959394'), Decimal('1.688401841437798245794341100')], 
'LQT': [Decimal('23034058.811000'), Decimal('45706418.420000'), 
Decimal('0.243186493430996726476558100')], 'SR': 
[Decimal('8389288.802664'), Decimal('0393135.373964'), 
Decimal('-2.146515189191049793306943120')], 'MIS7': 
[Decimal('6382868.080000'), Decimal('5336228.320000'), 
Decimal('-4.879627090905261579809913330')], 'LQ': 
[Decimal('98508613.709000'), Decimal('38822011.125000'), 
Decimal('-3.983067033313078110002846960')] }

這是我有錯誤的編碼:

    import locale
    locale.setlocale(locale.LC_ALL, '')
    for k in result.keys():
        if result[k] == result[k]:
            result[k] = locale.format('%d', result[k], grouping = True)
    if len(result) == 0:
        result["None"] = "None" 

我的錯誤字符串很長,但這看起來像主要位:

TypeError: %d format: a number is required, not list

我需要做什么? 謝謝

為什么不簡單:

    import locale
    locale.setlocale(locale.LC_ALL, '')
    for r in result:
        for k in result[r]:
            print locale.format('%2f', k, grouping = True) # Note there is not result[k]. Use your preferred number format instead of '%2f'

這是使用'%2f'的輸出:

23.034.058,811000
45.706.418,420000
0,243186
8.389.288,802664
393.135,373964
-2,146515
6.382.868,080000
5.336.228,320000
-4,879627
98.508.613,709000
38.822.011,125000
-3,983067
21.529.992,247811
75.922.363,959394
1,688402

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM