Python Forum
could not convert string to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
could not convert string to float
#3
(May-22-2020, 09:41 AM)ibreeden Wrote: As the message says: data is not a float. And also as the message shows: data is a lot of floats. That is the problem. You have to convert the text lines one by one to float. I guess something like (untested):
for dataline in data:
    print(float(dataline))

Thanks for the answer, but I am afraid it doesn't work, since it seems to convert element to element, if the first row is 2.25, float(data[3])=5.0, aditionally, the error

Error:
ValueError: could not convert string to float: '.'
appears, probably due to the fact that the code is converting each element by itself to float.

EDIT:
with open('posx_mean_no_acoplo_tf_multiple.txt', 'r') as f:
    content = f.readlines()
content = [x.strip() for x in content] 
[float(i) for i in content]


print(content)

type(content)
seems to work
Reply


Messages In This Thread
could not convert string to float - by player1681 - May-22-2020, 09:32 AM
RE: could not convert string to float - by ibreeden - May-22-2020, 09:41 AM
RE: could not convert string to float - by player1681 - May-22-2020, 09:56 AM
RE: could not convert string to float - by buran - May-22-2020, 12:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 322 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 1,939 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 6,036 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,365 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,583 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,429 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,614 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,559 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,445 Apr-12-2022, 08:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

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