我有一个价格清单,我希望从中移除所有空格,例如prices[0] = '2673.00'
prices = ['2 673.00', '53.55', '1 478.00', ... ]
prices = [float(x) for x in prices]
我尝试了各种选择,但没有一个适合我。
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'
请建议一种可行的方法。感谢。
答案 0 :(得分:3)
如果给出了输入,这肯定会起作用:
import re
regexp = re.compile(r'\s+', re.UNICODE)
prices_norm = [regexp.sub('', p) for p in prices]
但更好的解决方案是不要用空格打印浮点数。只需在打印前更改locale
:
import locale
locale.setlocale(locale.LC_ALL, 'en_US')