2014-12-04 60 views
0

我是Python初學者,我正在做一個程序來將XML文件解析爲HTML文件。該文件是關於學生記錄,並且必須計算總分,並且我無法爲其製作功能。如果我的格式不正確,這是我第一次提出問題,非常抱歉。Python將XML解析爲HTML:不能通過類型'float'的非整型來乘序列

def overallScore(Weights,studentScore): 
     totalScore = 0 
     totalWeight = 0 
     for i in range(0,len(Weights)): 
      Weights[i] = float(Weights[i]) 
      totalScore = totalScore + studentScore[i]*Weights[i] 
      totalWeight = totalWeight + Weights[i] 
     overall = totalScore/totalWeight 
     overall = "%.2f" % overall 
     overallString = ' <p class="overall">Overall : ' + overall + '</p>\n' 
     return overallString 
    import xml.sax 
    def characters(self,data): 
     if (self.inRecords): 
      if (self.inStudents): 
       if (self.inStudent): 
        if(self.inName): 
         self.studentNames.append(data.strip()) 
        elif (self.inUsername): 
         self.studentUser.append(data.strip()) 
        elif (self.inScores): 
         self.studentScore.append(data.strip().split(",")) 
      if (self.inCourse): 
       if (self.inTitles): 
        self.Titles = data.strip().split(",") 
       elif (self.inWeights): 
        self.Weights = data.strip().split(",") 

self.outfile.write(overallScore(self.Weights,self.studentScore)) 

當我運行我得到這個錯誤信息的程序:

totalScore = totalScore + studentScore[i]*Weights[i] 
TypeError: can't multiply sequence by non-int of type 'float' 

我嘗試將其轉換爲整數或浮點數,但他們都沒有工作。 這裏是xml文件:

<?xml version="1.0" ?> 
<records> 
    <students> 
    <student> 
     <name>Sergey Brin</name> 
     <username>brinsergey</username> 
     <scores>0.0,90.0,95.0,94.0,0.0,99.0,92.0</scores> 
    </student> 
    <student> 
     <name>Steve Wosniak</name> 
     <username>signedbywoz</username> 
     <scores>85.0,93.0,98.0,96.0,98.0,100.0,100.0</scores> 
    </student> 
    </students> 
    <course> 
    <titles>lab1,lab2,hw1,exam1,lab3,hw2,exam2</titles> 
    <weights>3.0,3.0,4.0,6.0,3.0,4.0,10.0</weights> 
    </course> 
</records> 

回答

0

studentScore[i]listtuple,而Weights[i]float;如錯誤所述,您不能將listtuple乘以float

嘗試將print(studentScore)置於overallScore的頂部,以便您可以確切地看到它是什麼。

相關問題