使用语言环境在python中进行格式化

时间:2017-09-11 15:39:36

标签: python dictionary 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

我需要做什么? 谢谢

1 个答案:

答案 0 :(得分:0)

为什么不简单:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import injectTapEventPlugin from 'react-tap-event-plugin';
import LoginPage from './LoginPage.js'

import {RaisedButton, Dialog, FlatButton} from 'material-ui';


injectTapEventPlugin();

//var LoginPage = require('./pages/LoginPage.js');
//class component
class App extends Component {

  render() {
    return (
      <MuiThemeProvider>
      <div>
      This is my page
      <LoginPage />
      </div>
      </MuiThemeProvider>
    );
  }
}
export default App;

,这是使用 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'