Post

PythonPython

d
divija reddy

Posted on 26th April 2024|96 views

0
votes

Python Could Not Convert String To Float

How to resolve the error “could not convert string to float: id”?

here is my code which is showing the same error called  “could not convert string to float: id”

import os,sys 

from scipy import stats 

import numpy as np 

f=open('data2.txt', 'r').readlines()

 N=len(f)-1 

for j in range(0,N): 

w=f[j].split() 

l1=w[1:8] 

l2=w[8:15] 

li1=[float(y) for y in l1] 

li2=[float(y) for y in l2] 

result=stats.ttest_ind(li1,li2) 

print result[1]

 

Answers
P
Surya kumar

Posted on 26th April 2024

I have cleaned your code this may work now

import os, sys 

from scipy import stats 

import numpy as np 

for index, line in enumerate(open('data2.txt', 'r').readlines()): 

w = line.split(' ') 

l1 = w[1:8] 

l2 = w[8:15] 

try: 

li1 = map(float, l1) 

li2 = map(float, l2) 

except ValueError: 

print 'Line {j} is corrupt!'.format(j = index)' 

break 

result = stats.ttest_ind(li1, li2) 

print result[1]

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos