Home > front end >  ValueError: could not convert string to float: '.' Python
ValueError: could not convert string to float: '.' Python

Time:11-11

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

CodePudding user response:

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
  • Related