转换小数点 [英] convert decimal mark

查看:113
本文介绍了转换小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要读取数据的CSV文件,我想将其读取到Python中.我得到包含像"2,5"这样的字符串的列表.现在,执行float("2,5")不起作用,因为它的小数点错误.

I have a CSV file with data reading that I want to read into Python. I get lists that contain strings like "2,5". Now doing float("2,5") does not work, because it has the wrong decimal mark.

如何以2.5的形式将其读入Python?

How do I read this into Python as 2.5?

推荐答案

float("2,5".replace(',', '.'))在大多数情况下都可以使用

float("2,5".replace(',', '.')) will do in most cases

如果value很大且.已被使用数千,则可以:

If valueis a large number and .has been used for thousands, you can:

将所有逗号替换为点:value.replace(",", ".")

Replace all commas for points: value.replace(",", ".")

删除除最后一点以外的所有内容:value.replace(".", "", value.count(".") -1)

Remove all but the last point: value.replace(".", "", value.count(".") -1)

这篇关于转换小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆