How to remove thousands separator dot from the formatted numbers
How can I remove the dot separator from formatted numbers? I'm getting a list from a website by using regular expression:
a = [10.000, 20.000, 25.000]
How can I change them to a = [10000, 20000,开发者_Python百科 25000]
? (They're integers now).
I am assuming what you have are strings, since you got them from a website, they should be text at first
>>> a=["10.000","20.000","25.000"]
>>> [ i.replace(".","") for i in a ]
['10000', '20000', '25000']
Use replace as this:
mystring.replace('.', '')
If you have a locale defined on your machine that uses dot for the thousands separator, then you can use locale.atoi:
import locale
locale.setlocale(locale.LC_ALL, 'en_DK.utf8')
a=['10.000','20.000','25.000']
a=map(locale.atoi,a)
print(a)
# [10000, 20000, 25000]
To see what locales are installed on your machine, (at least on unix), run
locale -a
In my opinion, I would remove the dots before turning them into numbers. Instead of doing int(num_in_string)
do this: int(num_in_string.replace('.', ''))
精彩评论