如何将从工作表扫描的浮动解析为包含逗号的文本?

txt = "1,903.44"
value = float(txt) # This fails due to ',' in string

更新:对不起,我不清楚。我正在使用jython 2.5,它没有语言环境模块。

有帮助吗?

解决方案

txt = "1,903.44"
value = float(txt.replace(',', ''))

如果你需要本地化,这不会真正起作用,但如果你知道逗号是你的分隔符就行了。

其他提示

之后使用 locale.atof() code> locale.setlocale(locale.LC_ALL,'')。

您可以删除逗号:

txt = txt.replace(',', '')
value = float(txt)
scroll top