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

前端 未结 8 1019
时光说笑
时光说笑 2020-11-22 08:10

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

>>> int(\"1,000,000         


        
8条回答
  •  春和景丽
    2020-11-22 08:42

    >>> 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 US.

提交回复
热议问题