howto remove the thousand separator

Duncan Booth duncan.booth at invalid.invalid
Fri Apr 19 11:04:57 EDT 2013


"pyth0n3r" <pyth0n3r at gmail.com> wrote:

> I came across a problem that when i deal with int data with ',' as
> thousand separator, such as 12,916, i can not change it into int() or
> float(). How can i remove the comma in int data?
> Any reply will be appreciated!!
> 

Parse it using the locale module, just be sure to set the correct locale 
first:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'English_United Kingdom.1252'
>>> locale.atoi('1,000')
1000
>>> locale.atof('1,000')
1000.0
>>> locale.setlocale(locale.LC_ALL, 'French_France')
'French_France.1252'
>>> locale.atof('1,000')
1.0


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list