Python 2.7及以上版本直接使用format设置千分位分隔符
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> format(1234567890, ',')
'1,234,567,890'
>>>
正则实现
import re
def number_format(s):
s = str(s)
while True:
(s, count) = re.subn(r"(\d)(\d{3})((:?,\d\d\d)*)$", r"\1,\2\3", s)
if count == 0 : break
return s
print number_format(1234567890)