ValueError: invalid literal for float():

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TMS
    New Member
    • Sep 2006
    • 119

    ValueError: invalid literal for float():

    I'm working on a linear regression program and when I try to convert the input file data to a float (it reads in as a string), I get this error. Perhaps someone can help me? I'm using Windows XP, and IDLE. (also the console screen).

    Code:
    #! /usr/bin/env python
    import sys
    import os
    from numpy import * 
    numLst = [] #an empty list
    if len(sys.argv) == 2:
    	infile = open(sys.argv[1], 'r')
    elif len(sys.argv) <2:
    	infile = raw_input("Name of file: ")
    elif len(sys.argv) > 2:
    	raise SyntaxError, "Too many arguments"
    data = open(infile, 'r')
    for x in data:
    	x = float(x) # HERE IS THE PROBLEM
    	numLst.append(x.strip('\n').split(" "))
     
    a = array(numLst)
    print a
    Input file looks like this:

    1.0 2.0
    2.0 3.5
    2.5 5.0
    3.5 6.5

    which are data points, x and y respectively.

    Thanks!

    TMS
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by TMS
    I'm working on a linear regression program and when I try to convert the input file data to a float (it reads in as a string), I get this error. Perhaps someone can help me? I'm using Windows XP, and IDLE. (also the console screen).

    Code:
    #! /usr/bin/env python
    import sys
    import os
    from numpy import * 
    numLst = [] #an empty list
    if len(sys.argv) == 2:
    	infile = open(sys.argv[1], 'r')
    elif len(sys.argv) <2:
    	infile = raw_input("Name of file: ")
    elif len(sys.argv) > 2:
    	raise SyntaxError, "Too many arguments"
    data = open(infile, 'r')
    for x in data:
    	x = float(x) # HERE IS THE PROBLEM
    	numLst.append(x.strip('\n').split(" "))
     
    a = array(numLst)
    print a
    Input file looks like this:

    1.0 2.0
    2.0 3.5
    2.5 5.0
    3.5 6.5

    which are data points, x and y respectively.

    Thanks!

    TMS
    Each iteration reads a line from the file. The argument to float must be in the correct format. See below:
    Code:
    >>> x = '1.0 2.0'
    >>> float(x)
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    ValueError: invalid literal for float(): 1.0 2.0
    >>> x,y = [float(i) for i in x.split()]
    >>> x
    1.0
    >>> y
    2.0
    >>>
    Separate the numbers and pass each one to float().

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by TMS
      I'm working on a linear regression program and when I try to convert the input file data to a float (it reads in as a string), I get this error. Perhaps someone can help me? I'm using Windows XP, and IDLE. (also the console screen).

      Code:
      #! /usr/bin/env python
      import sys
      import os
      from numpy import * 
      numLst = [] #an empty list
      if len(sys.argv) == 2:
      	infile = open(sys.argv[1], 'r')
      elif len(sys.argv) <2:
      	infile = raw_input("Name of file: ")
      elif len(sys.argv) > 2:
      	raise SyntaxError, "Too many arguments"
      data = open(infile, 'r')
      for x in data:
      	x = float(x) # HERE IS THE PROBLEM
      	numLst.append(x.strip('\n').split(" "))
       
      a = array(numLst)
      print a
      Input file looks like this:

      1.0 2.0
      2.0 3.5
      2.5 5.0
      3.5 6.5

      which are data points, x and y respectively.

      Thanks!

      TMS
      Not sure about your error message but if sys.argv == 2 then wouldn't you be opening the file twice? Probably nothing to do with the problem but just thought I'd mention it.

      Also, you didn't read the file. Try:
      Code:
      data = infile.read()
      I think that might work.

      Comment

      • TMS
        New Member
        • Sep 2006
        • 119

        #4
        ok, I see now. But I'm not sure how to use that with my for loop. Or do I use it in the for loop? Shouldn't I convert the file data to float before I append it to my list?

        Comment

        • TMS
          New Member
          • Sep 2006
          • 119

          #5
          Originally posted by ilikepython
          Not sure about your error message but if sys.argv == 2 then wouldn't you be opening the file twice?

          Also, you didn't read the file. Try:
          Code:
          data = infile.read()
          I think that might work.
          No.
          2 = the second argument in the file. Note the 'len' before sys.argv

          It reads the file fine. That isn't the problem. I'm trying to convert the data from strings to floats.

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by TMS
            No.
            2 = the second argument in the file. Note the 'len' before sys.argv

            It reads the file fine. That isn't the problem. I'm trying to convert the data from strings to floats.
            Oh sorry, but for the file opening I meant
            Code:
            if len(sys.argv) == 2:
            	[B]infile = open(sys.argv[1], 'r')[/B]
            elif len(sys.argv) <2:
            	infile = raw_input("Name of file: ")
            elif len(sys.argv) > 2:
            	raise SyntaxError, "Too many arguments"
            [B]data = open(infile, 'r')[/B]
            You have two open calls. I'm sorry if I'm being stupid but it doesn't really matter so forget about it.

            Comment

            • TMS
              New Member
              • Sep 2006
              • 119

              #7
              you aren't being stupid.
              My assignment requires that only one file is provided on the command line.

              Do you have a better way of doing it?

              In tests what I've done works fine. If I provide more than one file I get an error, which is what I want.

              Perhaps I'm missing something here. The first arg is 0, second is 1, etc. If the first arg is the program name, the second is the file name I want opened. So, the length of the args should equal 2 or there are two many args, right?

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by TMS
                ok, I see now. But I'm not sure how to use that with my for loop. Or do I use it in the for loop? Shouldn't I convert the file data to float before I append it to my list?
                Code:
                >>> numList = []
                >>> fList = open('data_file').readlines()
                >>> for x in fList:
                ... 	numList.append([float(i) for i in x.strip().split()])
                ... 	
                >>> numList
                [[1.0, 2.0], [2.0, 3.5], [2.5, 5.0], [3.5, 6.5]]
                >>>
                If you create a file object, don't forget to close it. Done like above, no file object is created.

                Comment

                • ilikepython
                  Recognized Expert Contributor
                  • Feb 2007
                  • 844

                  #9
                  Originally posted by TMS
                  you aren't being stupid.
                  My assignment requires that only one file is provided on the command line.

                  Do you have a better way of doing it?

                  In tests what I've done works fine. If I provide more than one file I get an error, which is what I want.

                  Perhaps I'm missing something here. The first arg is 0, second is 1, etc. If the first arg is the program name, the second is the file name I want opened. So, the length of the args should equal 2 or there are two many args, right?
                  Ok, sys.argv[1] is the file name. Right? If that's the case then I believe you should get an error message. I tried it:
                  Code:
                  >>> infile = open("C:\\testfile.txt", "r")
                  >>> data = open(infile, "r")
                  
                  Traceback (most recent call last):
                    File "<pyshell#5>", line 1, in <module>
                      data = open(infile, "r")
                  TypeError: coercing to Unicode: need string or buffer, file found
                  >>>
                  Did it work for you?
                  Please correct me if I am wrong?

                  Comment

                  • ghostdog74
                    Recognized Expert Contributor
                    • Apr 2006
                    • 511

                    #10
                    something you might want to take a look
                    Code:
                    ....
                    if len(sys.argv) == 2:
                    	data = open(sys.argv[1], 'r')
                    elif len(sys.argv) <2:
                    	infile = raw_input("Name of file: ")
                            data = open(infile, 'r')
                    elif len(sys.argv) > 2:
                    	raise SyntaxError, "Too many arguments"
                    for x in data:
                    ....
                    sys.argv[0] is the program name, argv[1] is the first argument and so on.

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by TMS
                      ok, I see now. But I'm not sure how to use that with my for loop. Or do I use it in the for loop? Shouldn't I convert the file data to float before I append it to my list?
                      Here's a one-liner:
                      Code:
                      >>> [[float(x),float(y)] for x,y in [j.split() for j in open('data.txt').read().split('\n')]]
                      [[1.0, 2.0], [2.0, 3.5], [2.5, 5.0], [3.5, 6.5]]
                      >>>

                      Comment

                      • TMS
                        New Member
                        • Sep 2006
                        • 119

                        #12
                        YEAH!!! thank you. Now I can get down to the complexities of linear regression, which is more busy work than anything. Thank you!

                        This is what it looks like right now, there are no error messages:

                        Code:
                        #! /usr/bin/env python
                        import sys
                        import os
                        from numpy import * 
                        numLst = [] #an empty list
                        if len(sys.argv) == 2:
                        	infile = open(sys.argv[1], 'r')
                        	for x in infile:
                        		numLst.append([float(i) for i in x.strip().split()]) 
                        elif len(sys.argv) <2:
                        	infilename = raw_input("Name of file: ")
                        	data = open(infilename, 'r')
                        	for x in data:
                        		numLst.append([float(i) for i in x.strip().split()])
                        elif len(sys.argv) > 2:
                        	raise SyntaxError, "Too many arguments"
                        a = array(numLst)
                        print a
                        b = a[0:, 1] #this is just me manipulating the list a bit
                        print b
                        c = a[0:,1]
                        print
                        bA = b[:1]
                        g = bA
                        print numLst
                        Originally posted by bvdet
                        Code:
                        >>> numList = []
                        >>> fList = open('data_file').readlines()
                        >>> for x in fList:
                        ... 	numList.append([float(i) for i in x.strip().split()])
                        ... 	
                        >>> numList
                        [[1.0, 2.0], [2.0, 3.5], [2.5, 5.0], [3.5, 6.5]]
                        >>>
                        If you create a file object, don't forget to close it. Done like above, no file object is created.

                        Comment

                        Working...