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: 425)
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 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 291 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 1,914 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,996 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,343 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,548 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,878 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,407 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,546 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,525 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,409 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