Bottom Page

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: 7)
Quote
#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))
Quote
#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
Quote
#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
Quote
#5
with open('posx_mean_no_acoplo_tf_multiple.txt', 'r') as f2:
    data = [float(line) for line in f2]
player1681 likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I convert this string back to a list of integers? donmerch 6 312 Apr-05-2020, 06:43 PM
Last Post: donmerch
  could not convert string to float: C JamesW 2 724 Jan-29-2020, 04:56 PM
Last Post: JamesW
  Python convert multi line into single line formatted string karthidec 2 507 Dec-23-2019, 12:46 PM
Last Post: karthidec
  How to check if user entered string or integer or float?? prateek3 5 5,021 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  how to convert list to string after append !! evilcode1 3 340 Dec-16-2019, 09:50 AM
Last Post: buran
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 761 Nov-29-2019, 06:19 AM
Last Post: ThomasL
  convert integers to a string tantony 1 440 Oct-04-2019, 06:40 PM
Last Post: ichabod801
  ValueError: could not convert string to float: . BoaCoder3 3 101,427 Aug-24-2019, 06:26 AM
Last Post: atlass218
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 538 Jul-25-2019, 11:32 PM
Last Post: scidam
  ValueError: could not convert string to float michaelbs007 1 3,651 Jun-08-2019, 08:44 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)