Python Forum
could not convert string to float: C
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
could not convert string to float: C
#1
Quote:Recently I have been working on a code and got stuck for days on this error. Basically the program takes a students HTML Transcript and converts it into a table created using DJANGO. The program used to work with no problems but after recent school changes in the students HTML Transcript file for students it keeps shooting out a error:

Exception Value: "Could not convert string to float:C ExceptionLocation C:\Users\CMPS\Desktop\AdvisementSystem\program\src\myproject\myproject\myapp\extractData.py in splitTR, line 14

The course numbers for all Courses switched for example, CMPS190 has changed to CMPS190B and ENG201 is now ENG201B.

How can I change the code to read that "B" in with the course number? Code is below:

 
from bs4 import BeautifulSoup
from CourseGrade import *

def splitTr(tr, grade):
    grade.department = tr.find_all('td')[0].text.lstrip()
    grade.course_num = tr.find_all('td')[1].text
    if tr.find_all('td')[2].text == "UG" or tr.find_all('td')[2].text == "GR":
        grade.course_title = tr.find_all('td')[3].text
        grade.grade = tr.find_all('td')[4].text.lstrip()
        grade.creditHour = int(float(tr.find_all('td')[5].text.lstrip()))
    else :
        grade.course_title = tr.find_all('td')[2].text
        grade.grade = tr.find_all('td')[3].text.lstrip()
        grade.creditHour = int(float(tr.find_all('td')[4].text.lstrip()))

def getDataList(student, fileName="Academic Transcript.html"):
    file = open(fileName,'r')
    soup = BeautifulSoup(file,"html.parser")

    studentName = soup.find("a", {"name":"Student Address"})
    if studentName != None:
        student.name = studentName.contents[0]
    else:
        studentName = soup.find("div","staticheaders").contents[0]
        student.name = studentName.split()[1]+ " " + studentName.split()[2]
    print(soup.find('td',attrs={'class':'dddefault','colspan':6}).contents[0])

    table = soup.find('table',attrs={'class':'datadisplaytable'})
    # print(table)
    # table_body = table.findAll('tbody')
    # print(table_body)
    trs = table.find_all('tr')
    count = 0
    term = ''
    department = ''
    course_title = ''
    grade = ''
    for tr in trs:
        if tr.find('span',attrs={'class':'fieldOrangetextbold'}):
            term = tr.span.text
            term = term.replace(":", "")
            term = term.replace("Term", "").lstrip()
            continue
        if tr.find('td',attrs={'class':'dddefault','colspan':4}) or tr.find('td',attrs={'class':'dddefault','colspan':5}):
            grade = CourseGrade()
            splitTr(tr, grade)
            grade.semester_completed = term
            appendValidData(student, grade) 
            count = count + 1

def appendValidData(student, grade):
    if grade.grade == 'A':
        student.gradeList.append(grade)
    if grade.grade == 'B':
        student.gradeList.append(grade)
    if grade.grade == 'C':
        student.gradeList.append(grade)
    if grade.grade == 'P':
        student.gradeList.append(grade)
    if grade.grade == 'TA':
        student.gradeList.append(grade)
    if grade.grade == 'TB':
        student.gradeList.append(grade)
    if grade.grade == 'TC':
        student.gradeList.append(grade)
    if grade.grade == 'TP':
        student.gradeList.append(grade)

def fillData(student,courseT):
    for grade in student.gradeList:
        gradeId = grade.department + grade.course_num
        if gradeId == courseT.courseId :
                courseT.grade = grade.grade
                if grade.semester_completed != "":
                    strList = grade.semester_completed.split(' ',3)
                    courseT.semester = strList[0]
                    if len(strList)>1:
                        courseT.year = strList[1]
                    else :
                        courseT.year = strList[0]
                    grade.isListed = True
Reply
#2
In line 14 you are using the .lstrip() on the table data (td) from a certain table row (tr). The lstrip() function strips characters to the left of its edit point so you end up with what WAS just numbers from the right side of the data. But now, you added a 'B' to the right side. Since you can't convert a string with letters into a number you get that error. Change that line to use slicing, regular expressions or whatever you prefer for string manipulation and chop off the appended 'B'.

EDIT: BTW, your appendValidData() function is a bit repetitive. A more pythonic version would be:

def appendValidData(student, grade):
    if grade.grade in ['A', 'B', 'C', 'P', 'TA', 'TB', 'TC', 'TP']:
        student.gradeList.append(grade)
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#3
The Code actually needs to incorporate B in order for the html file to be read
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 253 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 1,897 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,944 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,325 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,527 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,850 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,389 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,474 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,502 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,378 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