473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. written=str(currenttotal)
  2. squarerootfile.write(written)
  3. squarerootfile.write("\n")
  4.  
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()
  4.  
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
Dec 31 '10 #1
3 3191
bvdet
2,851 Expert Mod 2GB
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
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:
  5.     a=readline.fun(line)
  6.     plotdot(line,a)
  7.     line=line+1
  8.  
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
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

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

Similar topics

2
by: Edward K. Ream | last post by:
From the documentation for the string module at: C:\Python23\Doc\Python-Docs-2.3.1\lib\module-string.html letters: The concatenation of the strings lowercase and uppercase described below....
4
by: Jason Chan | last post by:
i want to covert a querystring to float number in asp. what function should i use? i know cint() can convert string to int but how about float?
2
by: Chi Tang | last post by:
Hi, I try to convert a string to a float but it alway comes out with extra value. For example, the string input is '12.6' but the output is '12.6000003814697' The following is my code to do...
3
by: Wim | last post by:
I would like to store the ListView font in the XML config file. So I have a string variable listFont that stores the font (font.ToString()). It must be a string variable because a Font object...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
3
by: pipe.jack | last post by:
I'm trying to convert string to float and my float after conversion is 0 (zero). Here is my code: $c = $currencies->format($cart->show_total()); echo gettype($c); echo (float)$c; $c = 39.59...
1
by: tomerd11 | last post by:
please halp me to convert string to float
3
by: AmRita1979 | last post by:
Hello, i wrote a client in C, that read value of voltages. I read, in output, this value like a string, for example: +3.91 +0.29 +2.27 +1.21 +2.45 +4.48 +3.14 +0.72 +1.12 +4.73 +0.58 +1.87 .........
5
by: tumblypooh | last post by:
I am trying to write a method that have this signature in C# string ConvertFloatToString(float floatNumber, int precision) if we call ConvertFloatToString (1234.3433,2) then the method should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.