如何将逗号分隔的数字更改为 int
How to change comma separated number to int
我注意到有一种方法可以将 an 更改为逗号分隔的数字 here。妙招!
我想知道是否有一种使用格式来扭转这种情况的方法,而不是像这样做:
import re
def comma_num_to_int(text):
n = re.sub(",", "", text)
return int(n)
my_big_number = "1,234,500"
print comma_num_to_int (my_big_number) # 1234500
最好使用 python std 库中的 locale
模块,而不是用 replace
.
替换逗号
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
'en_US.UTF8'
>>> my_big_number = "1,234,500"
>>> print(locale.atoi(my_big_number))
1234500
>>> print(type(locale.atoi(my_big_number)))
<class 'int'>
我注意到有一种方法可以将 an 更改为逗号分隔的数字 here。妙招!
我想知道是否有一种使用格式来扭转这种情况的方法,而不是像这样做:
import re
def comma_num_to_int(text):
n = re.sub(",", "", text)
return int(n)
my_big_number = "1,234,500"
print comma_num_to_int (my_big_number) # 1234500
最好使用 python std 库中的 locale
模块,而不是用 replace
.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
'en_US.UTF8'
>>> my_big_number = "1,234,500"
>>> print(locale.atoi(my_big_number))
1234500
>>> print(type(locale.atoi(my_big_number)))
<class 'int'>