pythonlocaledecimal-point

Convert decimal mark when reading numbers as input


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.

How do I read this into Python as 2.5?


Solution

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

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

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

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