I have some data that I copied and pasted from Wikipedia and am in the process of cleaning it. I want to convert all of the strings that are numeric to floats. For some reason the numeric strings are not being recognised by str.isnumeric() except for the first one '100.0'
for n,i in enumerate(PercentageWorldEmissions):
if str.isnumeric(i):
PercentageWorldEmissions[n] = float(i)
else:
continue
output:
['% CO2 emissions by country', 100.0, '29.55', '14.95', '9.57', '6.56', '4.95', '3.58', '2.15', '1.74', '1.73', '1.71', '1.58', '1.40', '1.38', '1.38', '1.28', '1.27', '1.16', '1.15', '1.10', '0.99', '0.94', '0.91', '0.84', '0.78', '0.76', '0.70', '0.68', '0.66', '0.64', '0.63', '0.56', '0.56', '0.55', '0.53', '0.44', '0.44', '0.40', '0.39', '0.34', '0.31', '0.28', '0.27', '0.27', '0.26', '0.25', '0.22', '0.22', '0.21']
where PercentageWorldEmissions looks identical to the output except the element 100.0 which was originally '100.0'
why is isnumeric not recognising the other strings? are there hidden characters that I cannot see? I tried the repr() method that I saw on another page but that did not reveal anything.
I also tried isdigit, isalnum, isdecimal all to no avail... any help greatly appreciated!
I don't take that the element 100.0 was originally '100.0', since
'100.0'
as well wouldn't have been recognized bystr.isnumeric()
. This is because the character.
(full stop) does not have the Unicode numeric property. I rather presume that100.0
was originally'100'
, whichisnumeric
.Your problem could be solved e. g. this way: