2012-02-10 108 views
2

我有一些字符串代表使用逗号或点来分隔数以及具有不同浮动分隔符的数字。例如:解析一个字符串以漂浮不同的分隔符

“22 000,76”, “22.000,76”, “22,000.76”, “1022000,76”, “-1,022,000.76”, “1022000”, “22个000,76 $”,“ $ 22 000,76“

如何将这些转换为Python中的浮点数?

在PHP我用的功能是这样的:http://docs.php.net/manual/sr/function.floatval.php#84793

+0

灿你向我们展示你需要的输出。我对你的例子有点困惑。 – RanRag 2012-02-10 12:14:34

+0

输出示例:22000.76,22000.76,22000.76,1022000.76,1022000.76,1022000,22000.76,22000.76 - 浮点数 – mikhail 2012-02-10 12:22:21

+0

@Bondarenko:那么“100,000”呢? – 2012-02-10 12:30:34

回答

4
import re 
import locale 

# Remove anything not a digit, comma or period 
no_cruft = re.sub(r'[^\d,.-]', '', st) 

# Split the result into parts consisting purely of digits 
parts = re.split(r'[,.]', no_cruft) 

# ...and sew them back together 
if len(parts) == 1: 
    # No delimeters found 
    float_str = parts[0] 
elif len(parts[-1]) != 2: 
    # >= 1 delimeters found. If the length of last part is not equal to 2, assume it is not a decimal part 
    float_str = ''.join(parts) 
else: 
    float_str = '%s%s%s' % (''.join(parts[0:-1]), 
          locale.localeconv()['decimal_point'], 
          parts[-1]) 

# Convert to float 
my_float = float(float_str) 
+0

请勿使用我的回答是,如果@Niklas B建议的“100,000”应该是100000. – westmark 2012-02-10 12:34:56

+0

使用locale.localeconv()['decimal_point']'确保'float'不会失败当前语言环境中的小数点不是“。”。 – jcollado 2012-02-10 12:39:19

+0

好点@jcollado。编辑我的答案。 – westmark 2012-02-10 12:42:39

0

假如你有最多2个十进制数字:

sign_trans = str.maketrans({'$': '', ' ':''}) 
dot_trans = str.maketrans({'.': '', ',': ''}) 

def convert(num, sign_trans=sign_trans, dot_trans=dot_trans): 
    num = num.translate(sign_trans) 
    num = num[:-3].translate(dot_trans) + num[-3:] 
    return float(num.replace(',', '.')) 

我测试它在你的例子:

>>> for n in nums: 
...  print(convert(n)) 
... 
22000.76 
22000.76 
22000.76 
1022000.76 
-1022000.76 
1022000.0 
22000.76 
22000.76