• Register
1 vote
26 views

Problem:

I am trying to run a program like

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]
    list1=[float(x) for x in l1]
    list2=[float(x) for x in l2]
    result=stats.ttest_ind(list1,list2)
    print result[1]

However, it is frequently returning the same error:

ValueError: could not convert string to float: id

Do you have any idea about this error?

12 7 7
15,250 points

1 Answer

2 votes
 
Best answer

Solution:

Somewhere in your text file, a line has the word id in it, which can't really be converted to a number.

Your test code works because of the word id isn't present in line 2.

If you want to catch that line, try this code. I cleaned your code up a tad:

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:
        list1 = map(float, l1)
        list2 = map(float, l2)
    except ValueError:
        print 'Line {i} is corrupt!'.format(i = index)'
        break

    result = stats.ttest_ind(list1, list2)
    print result[1]

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 8 views
8 views
Problem: Hello guys, I am new to python programming language I basically come from c language background. I am having a problem while working with python strings. Anyway, in my program, I have got a situation where I need to convert a float into a python string. For ... the float to string I am really confused about it waiting for some help as soon as possible this is getting me in bit trouble.
asked Jul 3 Gavin 15.3k points
1 vote
1 answer 14 views
14 views
Problem: To check whether the input is Floating point number or not in python I wrote this program: import re # check Floating point number def checked(floatnum): # pass the regular expression # and the string in search() method if(re.search(floatnum)): print( ... line 9, in checked if(re.search(floatnum)): TypeError: search() missing 1 required positional argument: 'string' Can anyone solve this?
asked Apr 28 samhaz 5k points
0 votes
1 answer 17 views
1 vote
2 answers 92 views
92 views
Problem : The program supposed to take in two names, and if they are the same length it should check if they are the same word. But i am getting below error in python python typeerror: not all arguments converted during string formatting
asked Oct 21, 2019 peterlaw 6.9k points