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

前端 未结 8 943
时光说笑
时光说笑 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:43

    I tried this. It goes a bit beyond the question: You get an input. It will be converted to string first (if it is a list, for example from Beautiful soup); then to int, then to float.

    It goes as far as it can get. In worst case, it returns everything unconverted as 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
    

提交回复
热议问题