• Register
1 vote
36 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

Please log in or register to answer this question.

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 9 views
9 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, 2020 Gavin 15.3k points
0 votes
1 answer 13 views
13 views
Problem: I have a dataframe created form a JSON output that looks like this: Total Revenue Average Revenue Purchase count Rate Date Monday 1,304.40 CA$ 20.07 CA$ 2,345 1.54 % The value stored are received as string from the JSON. I am trying to: 1) Remove all characters in the ... count columns as int I tried to do the following: df[column] = (df[column].str.split()).apply(lambda x: float(x[0]))
asked Jan 22 Mashhoodch 9.1k points
0 votes
1 answer 16 views
16 views
Problem: Which of the following function convert a string to a float in python?Explain key differences between numbers and character strings. Use built-in functions to convert between integers, floating point numbers, and strings. Types control what operations (or methods) can be performed on a given value.
asked Feb 16 Mashhoodch 9.1k points
0 votes
1 answer 16 views
16 views
Problem: The error ValueError: could not convert string to float: is caused by a parsing error in the function float () with a string argument which can not be parsed as a float number. The float () function is used to convert a string with a float value to a float value. The float function parses the string and converts it as a float number.
asked Dec 2, 2020 Mashhoodch 9.1k points
1 vote
1 answer 17 views
17 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, 2020 samhaz 5k points
0 votes
1 answer 19 views
0 votes
1 answer 29 views
29 views asked Oct 23, 2020 Shaad Sh 800 points
0 votes
1 answer 39 views
39 views
Problem Hey, I want to send a string to my serial. Once If I get the string or array, how can I convert my string into float value? Answer me in easy-to-understand manner as possible as you can.
asked Dec 23, 2020 chris jordan 2.4k points
0 votes
1 answer 3 views
3 views
Problem: The program is supposed to take in two names, and if they are the same length it should check if they are the same word. If it's the same word it will print "The names are the same". If they are the same length but with different letters it will print "The names are different ... than '{1}'"% name1, name2) elif len(name1) < len(name2): print ("'{0}'is longer than '{1}'"% name2, name1)
asked Feb 16 Mashhoodch 9.1k points
1 vote
2 answers 104 views
104 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