2011-01-04 21 views
1

nは、typeフラグの文字列書式設定で正確に何が行われるのか理解しようとしています。文字列書式設定のPython

PEP 3101は(可能な整数型の項で)言う:

This is a large number with formatting applied: 1384309238430 

です:私は出力を得る

print "This is a large number with formatting applied: {0:n}".format(1384309238430) 

'n' - Number. This is the same as 'd', except that it uses the 
       current locale setting to insert the appropriate 
       number separator characters. 

は、私は次のコードを試してみました、番号区切り文字はありません。私のロケール設定を見つけるにはどうすればいいですか?どのように私は区切り文字を取得するのですか(私は、区切り文字で、それは千単位の区切り記号のようなものを参照していると思っています)。

答えて

10
import locale 
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') 
print('{0:n}'.format(1234)) 

利回り

1,234 

あなたはlocale.getlocale()とあなたの現在のロケールを見つけることができます:locale.getdefaultlocale()

In [31]: locale.getlocale() 
Out[31]: ('en_US', 'UTF8') 

とデフォルトのロケールを。

オン* nixシステムでは、locale -aというコマンドを使用して、マシンが認識しているロケールのリストを取得できます。

3

これは、すべてのロケールに依存します:

>>> print "{0:n}".format(134.3) 
134.3 
>>> import locale 
>>> locale.getlocale() 
(None, None) 
>>> locale.setlocale(locale.LC_ALL, 'de_DE') 
'de_DE' 
>>> print "{0:n}".format(134.3) 
134,3 
>>> print "{0:n}".format(13423.3) 
13423,3 
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') 
'en_US.UTF-8' 
>>> print "{0:n}".format(13423.3) 
13,423.3 
>>> 
1

localeモジュールをチェックしてください。 getdefaultlocaleメソッドは、通常のシステム設定をデフォルトにしたい場合に便利です。

1

localeパラメータにはおそらく空の文字列を使用してsetlocaleを呼び出す必要があります。

関連する問題