Solving problem is about exposing yourself to as many situations as possible like How to convert a string to a number if it has commas in it as thousands separators? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How to convert a string to a number if it has commas in it as thousands separators?, which can be followed any time. Take easy to follow this discuss.
I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?
I could replace the commas with empty strings before I try to convert it, but that feels wrong somehow. Is there a better way?
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
There are several ways to parse numbers with thousands separators. And I doubt that the way described by @unutbu is the best in all cases. That’s why I list other ways too.
The proper place to call
__main__module. It’s global setting and will affect the whole program and even C extensions (although note that LC_NUMERIC setting is not set at system level, but is emulated by Python). Read caveats in documentation and think twice before going this way. It’s probably OK in single application, but never use it in libraries for wide audience. Probably you shoud avoid requesting locale with some particular charset encoding, since it might not be available on some systems.
Use one of third party libraries for internationalization. For example PyICU allows using any available locale wihtout affecting the whole process (and even parsing numbers with particular thousands separators without using locales):
Write your own parsing function, if you don’t what to install third party libraries to do it “right way”. It can be as simple as
int(data.replace(',', ''))when strict validation is not needed.
Replace the commas with empty strings, and turn the resulting string into an
int or a
'1,000,000' int(a.replace(',' , '')) 1000000 float(a.replace(',' , '')) 1000000.0a =
(A dirty but quick way)
'-1,234,567,89.0123' "".join(a.split(",")) '-123456789.0123'a=
I got locale error from 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
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).string) try: return locale.atoi(output) except ValueError: try: return locale.atof(output) except ValueError: return output
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.
#python3 tenzin def changenum(data): foo = "" for i in list(data): if i == ",": continue else: foo += i return float(int(foo))