>>> a = '1,000,000'
>>> int(a.replace(',', ''))
1000000
>>>
Answer from joaquin on Stack Overflow>>> a = '1,000,000'
>>> int(a.replace(',', ''))
1000000
>>>
There's also a simple way to do this that should handle internationalization issues as well:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.atoi("1,000,000")
1000000
>>>
I found that I have to explicitly set the locale first as above, otherwise it doesn't work for me and I end up with an ugly traceback instead:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/locale.py", line 296, in atoi
return atof(str, int)
File "/usr/lib/python2.6/locale.py", line 292, in atof
return func(string)
ValueError: invalid literal for int() with base 10: '1,000,000'
I ran into a problem of providing comma separated integer to function while practising variable argument
This is the function
def multiply_num(*numbers):
print(numbers)
product = 1
for num in numbers:
product = product * num
return productNormally I would provide the value while calling the function like this
print(multiply_num(3,7,9,2))
But I had problem providing such value through the input() command. After some tinkering this worked
num = list(map(int, input("Enter numbers: ").split(",")))
print(multiply_num(*num))Even though it worked, it kind of feels like a hack, is there a better method.
convert string numbers separated by comma to integers or floats in python - Stack Overflow
Polars - Cast string to int, number with comma being null
python - How to convert a string to a number if it has commas in it as thousands separators? - Stack Overflow
python - How to convert an integer to a comma separated string - Stack Overflow
You'll want to split the string at the commas using str.split. Then, convert them to floats (I don't know why you're using int when you say that you want to convert them to "an int or float").
total = 0
s = '2, 3.4, 5, 3, 6.2, 4, 7'
for i in s.split(','):
total += float(i)
print total
Personally, I would prefer to do this with a generator expression:
s = '2, 3.4, 5, 3, 6.2, 4, 7'
total = sum(float(i) for i in s.split(','))
print total
The reason what you're doing doesn't work is that for i in s iterates over each individual character of s. So first it does total += int('2'), which works. But then it tries total += int(','), which obviously doesn't work.
You have a string of comma separated float values and not int. You need to split them first and then add them. You need to cast it to float and not int
total = 0
s = '2, 3.4, 5, 3, 6.2, 4, 7'
for i in s.split(','):
total += float(i)
print total
Output will be 30.6
I have a series with numbers that are coming back as str which I'm converting to int and it's showing a number with a comma as null. I'm not sure how to avoid this.
Example:
| 131 |
|---|
| 302 |
| 3,940 |
I've done the following to convert this data to int.
df.with_column(pl.col('column').cast(pl.Int64, strict=False))I get this as a result as int:
| 131 |
|---|
| 302 |
| null |
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
setlocale()is in__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):
NumberFormat.createInstance(Locale('en_US')).parse("1,000,000").getLong()
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.
my_str = '1,255,000'
my_num = int(my_str.replace(',','')) #replace commas with nothing
this will return my_num = 1255000
result = my_num * 2
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
my_str = locale.format("%d", result, grouping=True)
this will return->my_str='2,510,000'
The first part is easy:
temp = "8,741,291".replace(',', '')
n = int(temp) * 2
I thought getting the commas back is a little harder, but it's really not!
If you are using a recent version of Python you can use the new .format() string method like so:
s = "{0:,}".format(n)
If you are using Python more recent than 2.6 you can omit the 0 from the curly braces in this example. (Alas, I must use Cygwin, and alas, it only gives 2.6, so I'm used to typing the 0.)
The specification mini-language for the .format() method is here:
http://docs.python.org/library/string.html#formatstrings
@user1474424 explained the locale.format() function, which is cool; I didn't know about that one. I checked the docs; this has been around since Python 1.5!
http://docs.python.org/library/locale.html
You can do it with list comprehensions and the int factory function:
[ int(i) for i in csvnumber[0].split(',') ]
Example
>>> csvnumber=['23,43,41,21,34']
>>> [ int(i) for i in csvnumber[0].split(',') ]
[23, 43, 41, 21, 34]
x= ['23,43,41,21,34']
t=list(map(int, x[0].split(',')))
print (t)
Assume your list has only one element as your example.Output:
>>>
[23, 43, 41, 21, 34]
>>>
Then reach each element in the list t with a for loop and append them to your list num.