Python Forum
Error in the code ->ValueError: could not convert string to float: ' '
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in the code ->ValueError: could not convert string to float: ' '
#1
This is the code:
fichero = open('cuerpos.txt')

doc = fichero.readlines()

for t in range(3, len(doc)):
    fila = doc[t].split('\n')

    if (len(fila) == 2):
        for i, e in enumerate(fila):
            v = (4 * 3, 14159265 / 3)*[b](float(fila[1]) / 2)[/b] * 2
            m = v * float(fila[0])
            print('Esfera de volumen', '%4.3f' % (v), 'y masa', '%4.3f' % (m))


    if (len(fila) == 3):
        for i, e in enumerate(fila):
            v = 3, 14159265 * [b]float(fila[1]) ** 2 * float(fila[2])[/b]
            m = v * float(fila[0])
            print('Cilindro de volumen', '%4.3f' % (v), 'y masa', '%4.3f' % (m))


    if (len(fila) == 4):
        for i, e in enumerate(fila):
            v =[b] float(fila[1]) * float(fila[2]) * float(fila[3])[/b]
            m = v *[b] float(fila[0])[/b]
            print('Paralelepipedo de volumen', '%4.3f' % v, 'y masa', '%4.3f' % m)
Reply
#2
After fila = doc[t].split('\n') insert line print(fila) to look what fila is.
If float(fila[?]) throws an error than fila[?] is not a string which consists of digits.
I´m sure that the .split('\n') is false. Are you sure you want to split on end-of-line/carriage-return ?
Please show how your .txt file is structured.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,148 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  convert string to float in list jacklee26 6 1,813 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,901 Dec-17-2022, 12:24 AM
Last Post: snippsat
  openpyxl convert data to float jacklee26 13 5,705 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,234 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
  KivyMD App to APK Convert Error Nick_MC 0 2,264 Jul-18-2022, 08:59 AM
Last Post: Nick_MC
  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

Forum Jump:

User Panel Messages

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