问题
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
?
回答1:
float("2,5".replace(',', '.'))
will do in most cases
If value
is 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)
回答2:
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.
回答3:
Pandas supports this out of the box:
df = pd.read_csv(r'data.csv', decimal=',')
See http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
回答4:
using a regex will be more reliable
import re
decmark_reg = re.compile('(?<=\d),(?=\d)')
ss = 'abc , 2,5 def ,5,88 or (2,5, 8,12, 8945,3 )'
print ss
print decmark_reg.sub('.',ss)
result
abc , 2,5 def ,5,88 or (2,5, 8,12, 8945,3 )
abc , 2.5 def ,5.88 or (2.5, 8.12, 8945.3 )
If you want to treat more complex cases (numbers with no digit before the decimal mark for exemple) the regex I crafted to detect all types of numbers in the following thread may be of interest for you:
stackoverflow.com/questions/5917082/regular-expression-to-match-numbers-with-or-without-commas-and-decimals-in-text/5929469
回答5:
Try replacing all the decimal commas with decimal dots:
floatAsStr = "2,5"
floatAsStr = floatAsStr.replace(",", ".");
myFloat = float(floatAsStr)
The function replace
, of course, work on any substring as python does now differentiate between char and string.
回答6:
First you must ensure what locale was used to provide the number. Failing to do this random problems surely will occur.
import locale
loc = locale.getlocale() # get and save current locale
# use locale that provided the number;
# example if German locale was used:
locale.setlocale(locale.LC_ALL, 'de_DE')
pythonnumber = locale.atof(value)
locale.setlocale(locale.LC_ALL, loc) # restore saved locale
来源:https://stackoverflow.com/questions/7106417/convert-decimal-mark