muazfaiz
muazfaiz

Reputation: 5031

Remove \xa0 from string using in Python

I have a list of prices from which I want to remove all space such that the prices[0] = '2673.00'

prices = ['2 673.00', '53.55', '1 478.00', ... ]
prices = [float(x) for x in prices]

I have tried various options but none has worked for me.

x = str(prices[0]).replace(' ', '') # Got error --> ValueError: could not convert string to float: '2\xa0673.00'

import unicodedata
my_str = unicodedata.normalize("NFD", str(prices[0])) # tried  ‘NFC’, ‘NFKC’, ‘NFD’, and ‘NFKD’ as different forms but got same error as above

x = str(prices[0]).replace(u'\xa0', u'')  # Got error --> ValueError: could not convert string to float: '2\xa0673.00'

Please suggest a possible way. Thanks.

Upvotes: 1

Views: 4057

Answers (1)

Liran Funaro
Liran Funaro

Reputation: 2878

If the input is given, this will surely work:

import re
regexp = re.compile(r'\s+', re.UNICODE)
prices_norm = [regexp.sub('', p) for p in prices]

But if you have control over the production of the prices, a better solution will be not to print the floats with spaces. Just change the locale before you print them:

import locale
locale.setlocale(locale.LC_ALL, 'en_US')

Upvotes: 3

Related Questions