How to convert string with letters to float?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Haag
    New Member
    • Dec 2010
    • 3

    How to convert string with letters to float?

    I am trying to do a little bit of numeric computing. I wrote a program that writes the result of each iteration of a function to a file. I want to graph these results as dots using vpython, but I am unable to do so.

    I need each value to be on its own line in the text file, so I am using this to write the value to the text file:
    Code:
    written=str(currenttotal)
    squarerootfile.write(written)
    squarerootfile.write("\n")
    currenttotal is the value produced by the function
    squarerootfile is the object for the file it is written to

    Then I run the text file through another program that plots the line number as the x coordinate and the value as the y coordinate. The program reads the file using this:

    Code:
    path="c:\\pythonfiles2\\totalsqrt32.txt"
    input=file(path)
    input.readline()
    The value is returned as '0.5\n'. I need to convert this to the float 0.5. Is there a command for this? Or do I need to change the first program to not write strings? How do I do this and get it to put the next value on the next line?

    Thank you,

    Gordon
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Strings are written to and read from a file, so you are doing it correctly. You need to type cast the string you read from the file. I would suggest iteration on the file object instead of using readline(). Iteration will cease when you reach the end of the file. Use built-in function enumerate(), and you also have your line number.
    Code:
    fileObj = open(path)
    for i, line in enumerate(fileObj):
        line_number = i
        value = float(line.strip())

    Comment

    • Gordon Haag
      New Member
      • Dec 2010
      • 3

      #3
      I tried to do what you said, but the way you use i confuses me. Does 'for i, line in...' tell python that i is a line in ...? What does line.strip do?

      Here is pseudo-code of what I am trying to do.

      Code:
      path='c:\\pythonfiles2\\totalsqrtdone.txt'
      fun=file(path, "r")
      line=1
      while line<100000000:
          a=readline.fun(line)
          plotdot(line,a)
          line=line+1
      The problem is that a comes back as 'a\n'.

      I am using vpython to do the plotting.

      I am really new to programming, all help is appreciated.

      Comment

      • Gordon Haag
        New Member
        • Dec 2010
        • 3

        #4
        I figured it out.

        This loop prints the way I need to:
        Code:
            while abs(b**2-a)>e and steps<10000.:
                steps=steps+1
                b=(b+a/b)/2
                currenttotal=currenttotal+b
                written=currenttotal
                print>>squarerootfile, written
                print steps
        This prints with \n after each entry:
        Code:
            while abs(b**2-a)>e and steps<10000.:
                steps=steps+1
                b=(b+a/b)/2
                currenttotal=currenttotal+b
                written=str(currenttotal)
                squarerootfile.write(written)
                squarerootfile.write("\n")
                print steps
        Now all I need to do is rerun the calculaltion.
        Last edited by bvdet; Jan 1 '11, 01:51 PM. Reason: Add code tags

        Comment

        Working...