Python Forum
coult not convert string to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coult not convert string to float
#1
Hello,

I got this error while trying to compile my code:
Error:
  self.datayf.append(float(y.strip().replace(',', ''))) ValueError: could not convert string to float: '\\begin{figure}[h!]'
the thing is: there is no 
'\\begin{figure}[h!]'
 in the previous mentioned code line:

self.datayf.append(float(y.strip().replace(',', '')))
However 
 '\\begin{figure}[h!]'
  was mentioned previously here:


imgfigtabularstr = '\\begin{figure}[h!]\n\\centering\n\\begin{tabular}{cc}\n' 



What could that be?
Reply
#2
I don't know what's with the formatting in your post but have you tried printing
print(y.strip().replace(',', ''))
to see what it is that float is unable to turn from a string to a float.
Reply
#3
(Oct-10-2016, 09:43 PM)Yoriz Wrote: I don't know what's with the formatting in your post but have you tried printing
print(y.strip().replace(',', ''))
to see what it is that float is unable to turn from a string to a float.

Now I did. I wrote this:
 print(float(y.strip().replace(',', ''))) 
I got the same error:
Error:
ValueError: could not convert string to float: '\\begin{figure}[h!]'

(Oct-10-2016, 09:43 PM)Yoriz Wrote: I don't know what's with the formatting in your post.
Sorry for my writing typos.
Reply
#4
You will get the same error if you include the float conversion in it, remove the float to just print the value that is trying to be converted as shown in my previous post.
Reply
#5
(Oct-10-2016, 09:58 PM)Yoriz Wrote: You will get the same error if you include the float conversion in it, remove the float to just print the value that is trying to be converted as shown in my previous post.

Thank you Yoriz. You are right. So after removing the "float" thing, I got this error:

Error:
Traceback (most recent call last):   File "<pyshell#65>", line 1, in <module>     print(y.strip().replace(',', '')) NameError: name 'y' is not defined
Reply
#6
You cannot run that line only, you need the rest of the code that includes the defining of y
Reply
#7
(Oct-10-2016, 10:17 PM)Yoriz Wrote: You cannot run that line only, you need the rest of the code that includes the defining of y

Yes I also tried the "print" line in the code instead of: 
self.datayf.append(float(y.strip().replace(',', '')))
It compiled without errors. Anyway it is completely empty: the figure drawn and zero values returned by "print".
Reply
#8
(Oct-10-2016, 10:24 PM)kentman234 Wrote:
[color=#000000][size=small][font=monospace]self.datayf.append(float(y.strip().replace(',', '')))[/font][/size][/color]

Remove all text formatting before putting it in python tags. Select the text, and there is a button with an "A" with a minus symbol. You need to do this for code within python tags.

So then it will be
self.datayf.append(float(y.strip().replace(',', '')))
Recommended Tutorials:
Reply
#9
The idea is that you see what the output of the print line was.
If that is an empty string it is not possible for float to turn that into a number, so you need to decide if it should of been possible to be an empty string or if you need to deal with the exception caused by there being an empty string.
Reply
#10
(Oct-10-2016, 10:31 PM)metulburr Wrote:
(Oct-10-2016, 10:24 PM)kentman234 Wrote:
self.datayf.append(float(y.strip().replace(',', '')))

Remove all text formatting before putting it in python tags. Select the text, and there is a button with an "A" with a minus symbol. You need to do this for code within python tags.

So then it will be
self.datayf.append(float(y.strip().replace(',', '')))

OK, I try now:
print(y.strip().replace(',', ''))

(Oct-10-2016, 10:32 PM)Yoriz Wrote: The idea is that you see what the output of the print line was.
If that is an empty string it is not possible for float to turn that into a number, so you need to decide if it should of been possible to be an empty string or if you need to deal with the exception caused by there being an empty string.

So I think that our discussion has finished right? Because now I need to deal with the code itself.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,812 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,702 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,233 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,386 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,755 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,259 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,225 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,394 Feb-13-2022, 04:50 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020