Formatting question : printing numbers with thousands separators ?

Fred Pacquier fredp at mygale.org.nospam
Mon Sep 3 04:37:36 EDT 2001


juneaftn at orgio.net (June Kim) said :

> * and the most(or more or less) obfuscated way as a one-liner is:
> 
> def sep(s):  return
> re.sub(r"([-+]?\d{1,3}(\.\d*)?)(?=(\d{3})*(\.|$))",r",\1",s)[1:]

tanzer at swing.co.at (Christian Tanzer) said :

> How about:
>>>> import re
>>>> sep_1000_pat = re.compile("(\d{1,3}) (?= (?: \d\d\d)+ (?! \d) )",
>>>> re.X) sep_1000_pat.sub(r"\g<1>,", "2342342342344567") 
> '2,342,342,342,344,567'
> Short, but then you need Friedl's masterpiece on regular expression to
> understand it...

Ouch. Didn't know you could also write Perl in Python... :-))))))

-- 
YAFAP : http://www.multimania.com/fredp/



More information about the Python-list mailing list