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
#1
After reading a text file, as a column, I want to convert it from string to float; however, an error appears:

with open('posx_mean_no_acoplo_tf_multiple.txt', 'r') as f2:
    data = f2.read()
print(data)

data2=float(data)
    
print(type(data2))
Error:
ValueError: could not convert string to float: '2.25\n2.2695317544146922\n2.329339980428795\n2.4250625977456477\n2.5550797011698574\n2.71230173494984\n2.892967911772487\n3.0882634980597587\n3.293449789010904\n3.499272226187544\n3.701367185950187\n3.8913688480023603\n4.06653899594117\n4.220488053299581\n4.352979510931954\n4.460201719579866\n4.544721596733528\n4.605268242898987\n4.646847484315852\n4.670027623810766\n4.681257982348112\n4.681695512358507\n4.677770394738662\n4.669669116126706\n4.662148945229584\n4.6528774839607685\n4.643437850311123\n4.627777953202297\n4.603297500355522\n4.559669579586145\n4.489849558288886\n4.3795396286131405\n4.217820145014248\n3.9876937422929313\n3.675387059204849\n3.26353914389145\n2.733307933151497\n2.105160523649151\n1.5523910517699886\n1.1343162024113826\n0.8156488225000131\n0.5800645817088685\n0.4070368727014218\n0.2831923612679096\n0.19580025579018923\n0.13524269458388608\n0.09417950763477065\n0.06624645771006422\n0.04785919913604819\n0.03502215581410664\n0.026570015997405754\n0.020000851026048753\n0.015528673303797499\n0.011462300535368213\n0.008669991732948468\n0.005840011599713111\n0.004051213365511311\n0.002171916538622823\n0.0012444675811206312\n0.00027638904919229313\n4.4864963753136085e-38\n'
Can anybody help me?
I am attaching the text file just in case someone wants to take a look

Regads.

Attached Files

.txt   posx_mean_no_acoplo_tf_multiple.txt (Size: 1.18 KB / Downloads: 413)
Reply
#2
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))
Reply
#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
#4
with open('a.txt', 'r') as f2:
    data = f2.read()
data=(data.split("\n"))
data=[float(i) for i in data]
data
We first convert the string into list by using \n as delimiter. Then convert each element to float
Reply
#5
with open('posx_mean_no_acoplo_tf_multiple.txt', 'r') as f2:
    data = [float(line) for line in f2]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,813 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,707 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,236 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,757 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,277 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,228 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,395 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