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
?
To convert a whole number into a decimal, you must decide the result to a certain number of places after the decimal point and simply add a decimal and the number of zeros required to the right side of the whole number. For example: Convert 5 into decimal to the hundredth place. Given that the whole number is 5.
Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.
When you want the user to type in a decimal use float(input()) , if you want the user to type in an integer use int(input()) , but if you want the user to type in a string use input() .
You may do it the locale-aware way:
import locale # Set to users preferred locale: locale.setlocale(locale.LC_ALL, '') # Or a specific locale: locale.setlocale(locale.LC_NUMERIC, "en_DK.UTF-8") print locale.atof("3,14")
Read this section before using this method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With