Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: could not convert string to float: '.' Python

So I am trying to make sure that all the values that I have in the csv file are converted into float. The values in each cell inside the csv file are just numbers like for example "0.089" "23". For some reason when I try to run the code it is giving the following error, " ValueError: could not convert string to float: ‘.’ "
I can not really understand why the program is not reading the numbers from the csv file properly.

def loadCsv(filename):
    with open('BreastCancerTumor.csv','r') as f:
        lines = f.readlines()[1:]
    dataset = list(lines)
    for i in range(len(dataset)):
        dataset[i] = [float(x) for x in dataset[i]]
    return dataset

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You never split the line into comma-separated fields. So you’re looping over the characters in the line, not the fields, and trying to parse each character as a float. You get an error when you get to the . character.

Use the csv library to read the file, it will split each line into lists of fields.

import csv

def loadCsv(filename):
    with open('BreastCancerTumor.csv','r') as f:
        f.readline() # skip header
        csvf = csv.reader(f):

        dataset = [[float(x) for x in row] for row in csvf]
        return dataset
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading