Gerenuk Gerenuk - 1 month ago 6
Python Question

How can I change the (locale) thousands separator in Python to Arabic Unicode separator?

I'd like to change the thousands separator such that

{:,}.format(1234)
in Python uses a different character. The separator should be
'\u066c'
.

How can I set this without affecting any other locals settings?

EDIT: Any other suggestion for a unimposing separator viable in a fixed with font is welcome!

Answer

Your options are to either take the , formatted output and replace the commas, switch locales and use the 'n' number format (which will format the number based on the current locale), or use a third party library like babel. The latter gives you full locale control over number formatting, for example, provided there is a locale that uses U+066C as the thousands separator.

With the format() function, the first option is quite straight-forward really:

>>> format(1234, ',').replace(',', '\u066c')
'1٬234'

I have yet to find a locale that directly would use \u066c for Western Arabic numerals however; U+066C is commonly used only with Eastern Arabic numerals instead. Babel doesn't include any such locale data, at least.

Comments