close

[Solved] ValueError: could not convert string to float: ‘.’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: could not convert string to float: ‘.’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: could not convert string to float: ‘.’ Error Occurs?

Today I get the following error ValueError: could not convert string to float: ‘.’ in python.

How To Solve ValueError: could not convert string to float: ‘.’ Error ?

  1. How To Solve ValueError: could not convert string to float: '.' Error ?

    To Solve ValueError: could not convert string to float: '.' Error Obviously some of your lines don't have valid float data, specifically some line have text id which can't be converted to float.

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

    To Solve ValueError: could not convert string to float: '.' Error Obviously some of your lines don't have valid float data, specifically some line have text id which can't be converted to float.

Solution 1

Obviously some of your lines don’t have valid float data, specifically some line have text id which can’t be converted to float.

When you try it in interactive prompt you are trying only first line, so best way is to print the line where you are getting this error and you will know the wrong line e.g.

#!/usr/bin/python

import os,sys
from scipy import stats
import numpy as np

f=open('data2.txt', 'r').readlines()
N=len(f)-1
for i in range(0,N):
    w=f[i].split()
    l1=w[1:8]
    l2=w[8:15]
    try:
        list1=[float(x) for x in l1]
        list2=[float(x) for x in l2]
    except ValueError,e:
        print "error",e,"on line",i
    result=stats.ttest_ind(list1,list2)
    print result[1]

Solution 2


My error was very simple: the text file containing the data had some space (so not visible) character on the last line.

As an output of grep, I had 45  instead of just 45

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read