带空格的python格式字符串千位分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16670125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:19:13  来源:igfitidea点击:

python format string thousand separator with spaces

pythonstring-formatting

提问by wonzbak

For printing number with thousand separator, one can use the python format string :

要打印带有千位分隔符的数字,可以使用 python 格式字符串:

'{:,}'.format(1234567890)

But how can I specify that I want a space for thousands separator?

但是如何指定我想要一个用于千位分隔符的空间?

回答by Martijn Pieters

You'll have to use the 'n'locale-aware number format instead, and set your localeto one that uses a space as a thousands separator. Yes, this is painful.

您必须改用支持'n'区域设置的数字格式,并将您的区域设置设置为使用空格作为千​​位分隔符的区域设置。是的,这很痛苦。

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

'n'- 数字。这与 相同'd',不同之处在于它使用当前区域设置来插入适当的数字分隔符。

回答by Raz

Here is bad but simple solution if you don't want to mess with locale:

如果您不想惹麻烦,这是一个糟糕但简单的解决方案locale

'{:,}'.format(1234567890.001).replace(',', ' ')

回答by user136036

This will set the locale environment to your default OS location:

这会将语言环境设置为您的默认操作系统位置:

import locale
locale.setlocale(locale.LC_ALL, '')

Then you can use:

然后你可以使用:

"{0:n}".format(1234567890)

回答by stemd

Answer of @user136036 is quite good, but unfortunately it does not take into account reality of Python bugs. Full answer could be following:

@user136036 的回答很好,可惜没有考虑到 Python 的 bug 的真实性。完整的答案可能如下:

Variant A

变体A

If locale of your platform is working right, then just use locale:

如果您平台的语言环境正常工作,那么只需使用语言环境:

import locale
locale.setlocale(locale.LC_ALL, '')
print("{:,d}".format(7123001))

Result is dependent on your locale and Python implementation working right.

结果取决于您的语言环境和 Python 实现是否正常工作。

But what if Python formatting according to locale is broken, e.g. Python 3.5 on Linux?

但是如果根据语言环境的 Python 格式被破坏了,例如 Linux 上的 Python 3.5 呢?

Variant B

变体B

If Python does not respect grouping=Trueparameter, you can use locale and a workaround (use monetary format):

如果 Python 不尊重grouping=True参数,您可以使用区域设置和解决方法(使用货币格式):

locale.setlocale(locale.LC_ALL, '')
locale._override_localeconv = {'mon_thousands_sep': '.'}
print(locale.format('%.2f', 12345.678, grouping=True, monetary=True))

Above gives 12.345,68 on my platform. Setting monetary to False or omitting it - Python does not group thousands. Specifying locale._override_localeconv = {'thousands_sep': '.'}do nothing.

以上在我的平台上给出了 12.345,68。将货币设置为 False 或省略它 - Python 不会对数千人进行分组。指定locale._override_localeconv = {'thousands_sep': '.'}什么都不做。

Variant C

变体 C

If you don't have time to check what is working OK and what is broken with Python on your platform, you can just use regular string replace function (if you want to swap commas and dot to dots and comma):

如果您没有时间检查平台上哪些工作正常以及哪些 Python 损坏,您可以使用常规字符串替换功能(如果您想将逗号和点交换为点和逗号):

print("{:,.2f}".format(7123001.345).replace(",", "X").replace(".", ",").replace("X", "."))

print("{:,.2f}".format(7123001.345).replace(",", "X").replace(".", ",").replace("X", "."))

Replacing comma for space is trivial (point is assumed decimal separator):

用逗号代替空格很简单(点被假定为十进制分隔符):

print("{:,.2f}".format(7123001.345).replace(",", " ")

print("{:,.2f}".format(7123001.345).replace(",", " ")

回答by yvs93

The 1st answer miss one thing, if you are dealing with an object column and don't want to mess with locale:

如果您正在处理对象列并且不想弄乱语言环境,则第一个答案会错过一件事:

'{:,}'.format(1234567890.001).str.replace(',', ' ')