How to use Python to convert a string to a number if it has commas as thousands separators?

I have a line that represents a number that uses commas to separate thousands. How can I convert this number in python?

>>> int("1,000,000") 

Raises a ValueError .

I can replace commas with empty strings before I try to convert them, but this is somehow not the case. Is there a better way?

+64
python
Nov 22 '09 at 17:10
source share
8 answers
 import locale locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) locale.atoi('1,000,000') # 1000000 locale.atof('1,000,000.53') # 1000000.53 
+86
Nov 22 '09 at 17:20
source share

There are several ways to parse thousands delimited numbers. And I doubt that the method described by @unutbu is the best in all cases. This is why I list other ways.

  1. The correct place to call setlocale() is in the __main__ module. This is a global setting and will affect the entire program and even C extensions (although note that the LC_NUMERIC setting is not set at the system level, but is emulated by Python). Read the warnings in the documentation and think twice before taking this path. This is probably good in one application, but never use it in libraries for a wide audience. Perhaps you should avoid requesting a locale with a specific charset encoding, as it may not be available on some systems.

  2. Use one of the third-party libraries for internationalization. For example, PyICU allows you to use any available locale without affecting the entire process (and even analyze numbers with specific thousands separators without using locales):

    NumberFormat.createInstance (Locale ('en_US')). Syntactic ("1,000,000"). GetLong ()

  3. Write your own parsing function if you don’t need to install third-party libraries to do this β€œcorrectly”. It can be as simple as int(data.replace(',', '')) when strict checking is not required.

+32
Nov 23 '09 at 9:18
source share

Replace commas with empty strings and turn the resulting string into an int or float .

 >>> a = '1,000,000' >>> int(a.replace(',' , '')) 1000000 >>> float(a.replace(',' , '')) 1000000.0 
+8
Apr 18 '13 at 19:48
source share

It works:

(Dirty but quick way)

 >>> a='-1,234,567,89.0123' >>> "".join(a.split(",")) '-123456789.0123' 
+5
Mar 15 '13 at 11:49
source share

I got a locale error from the accepted answer, but the following change works here in Finland (Windows XP):

 import locale locale.setlocale( locale.LC_ALL, 'english_USA' ) print locale.atoi('1,000,000') # 1000000 print locale.atof('1,000,000.53') # 1000000.53 
+3
Oct 05 '12 at 12:15
source share

I have tried this. This is slightly beyond the scope of the question: You are getting input. First, it will be converted to a string (if it is a list, for example, from Beautiful soup); then to int, then swim.

He goes as far as possible. In the worst case, it returns everything uncured as a string.

 def to_normal(soupCell): ''' converts a html cell from beautiful soup to text, then to int, then to float: as far as it gets. US thousands separators are taken into account. needs import locale''' locale.setlocale( locale.LC_ALL, 'english_USA' ) output = unicode(soupCell.findAll(text=True)[0].string) try: return locale.atoi(output) except ValueError: try: return locale.atof(output) except ValueError: return output 
+1
Aug 23 '17 at 12:46 on
source share
 #python3 tenzin def changenum(data): foo = "" for i in list(data): if i == ",": continue else: foo += i return float(int(foo)) 
0
Aug 17 '17 at 19:32
source share
 >>> import locale >>> locale.setlocale(locale.LC_ALL, "") 'en_US.UTF-8' >>> print locale.atoi('1,000,000') 1000000 >>> print locale.atof('1,000,000.53') 1000000.53 

this is done on Linux in the USA.

-one
Jun 26 '15 at 13:20
source share



All Articles