By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

How to convert string with letters to float?

P: 3
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:
Expand|Select|Wrap|Line Numbers
  1. written=str(currenttotal)
  2. squarerootfile.write(written)
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. path="c:\\pythonfiles2\\totalsqrt32.txt"
  2. input=file(path)
  3. 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,

Dec 31 '10 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. fileObj = open(path)
  2. for i, line in enumerate(fileObj):
  3.     line_number = i
  4.     value = float(line.strip())
Dec 31 '10 #2

P: 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.

Expand|Select|Wrap|Line Numbers
  1. path='c:\\pythonfiles2\\totalsqrtdone.txt'
  2. fun=file(path, "r")
  3. line=1
  4. while line<100000000:
  6.     plotdot(line,a)
  7.     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.
Jan 1 '11 #3

P: 3
I figured it out.

This loop prints the way I need to:
Expand|Select|Wrap|Line Numbers
  1.     while abs(b**2-a)>e and steps<10000.:
  2.         steps=steps+1
  3.         b=(b+a/b)/2
  4.         currenttotal=currenttotal+b
  5.         written=currenttotal
  6.         print>>squarerootfile, written
  7.         print steps
This prints with \n after each entry:
Expand|Select|Wrap|Line Numbers
  1.     while abs(b**2-a)>e and steps<10000.:
  2.         steps=steps+1
  3.         b=(b+a/b)/2
  4.         currenttotal=currenttotal+b
  5.         written=str(currenttotal)
  6.         squarerootfile.write(written)
  7.         squarerootfile.write("\n")
  8.         print steps
Now all I need to do is rerun the calculaltion.
Jan 1 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.