Kristada673
Kristada673

Reputation: 3744

Why is Python showing 'ValueError: could not convert string to float'?

I have a CSV containing numbers which I am trying to convert to floats.

filename = "filename.csv"
enclosed_folder = "path/to/Folder"
full_path = os.path.join(enclosed_folder,filename)

with open(full_path) as input_data:
    temp = input_data.readlines()
    n = len(temp) #int(temp.pop(0))
    matrix = [x.split(" ") for x in temp]
    for i in range(n):
        for j in range(n):
            matrix[i][j] = float(matrix[i][j])
    input_data.close()

When I open the file in any text editor, it does not show the \n at the end of each row.

enter image description here

But running the python code shows the `ValueError: could not convert string to float' because of '\n' being present at the end of each row.

Traceback (most recent call last):
  File "hierarchical-clustering.py", line 37, in <module>
    matrix[i][j] = float(matrix[i][j])
ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429\n'

So, how do I fix this error?

EDIT: I used strip() as well as rstrip() as suggested in some of the answers to remove whitespaces, but still the error does not go away:

Traceback (most recent call last):
  File "hierarchical-clustering.py", line 37, in <module>
    matrix[i][j] = float(matrix[i][j].rstrip())
ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429'

Upvotes: 3

Views: 22252

Answers (3)

Robert
Robert

Reputation: 8767

The error is due to your line parsing. You are separating on spaces, not commas, which is what should happen according to your screenshot. The key is looking at the error returned. It is trying to convert the entire line from a string into a float.

Change:

matrix = [x.split(" ") for x in temp]

To:

matrix = [x.split(",") for x in temp]

Upvotes: 6

Ofer Sadan
Ofer Sadan

Reputation: 11922

Remove the newline char with rstrip() like this:

matrix[i][j] = float(matrix[i][j].rstrip())

Upvotes: 1

Shai
Shai

Reputation: 114786

you can use strip() to remove whitespaces from the string.

matrix[i][j] = float(matrix[i][j].strip())

If the commas are troubling you, you might want to .split(',') with commas and not spaces:

matrix = [x.strip().split(",") for x in temp]

Upvotes: 2

Related Questions