1
我目前正在開發一個簡單的演示程序,該演示程序讀取包含兩列的excel文件,並將列的每一行中的數據提取爲兩列不同的名單。TypeError:Float()參數在讀取列表時必須是字符串或數字
這一切工作正常,但是當我嘗試使用matplotlib繪製數據時,出現此錯誤。
TypeError: float() argument must be a string or a number
我知道錯誤是由第二個列表中的浮點值引起的,應該只是整數。我不明白的是如何防止它們在讀入列表中時變成浮動。
[text:u'Joe', text:u'Sam', text:u'Bobby', text:u'Tom', text:u'Jane']
[number:23.0, number:36.0, number:19.0, number:31.0, number:28.0]
我試過在幾個不同的地方進行類型轉換,但總是得到相同的錯誤。
TypeError: int() argument must be a string or a number, not 'Cell'
這裏是我使用這個程序
def bargraph(self, fileName, xlabel, title):
people = list()
dollars = list()
workbook = xlrd.open_workbook(fileName)
worksheet = workbook.sheet_by_index(0)
for row in range(worksheet.nrows):
people.append(worksheet.cell(row ,0))
for row in range(worksheet.nrows):
dollars.append(worksheet.cell(row, 1))
plt.bar(people, dollars, 1, color="blue")
plt.xlabel(xlabel)
plt.title(title)
plt.show()
在繪圖代碼之前添加'print(dollars)'並粘貼結果。 –
已發佈 – sbowde4
啊,我現在明白了。你有'Cell'對象(比如'number:23.0',而一個float看起來像'23.0'),因爲'worksheet.cell(row,1)'不會立即給你提供字符串。閱讀您的圖書館的文檔,並瞭解如何從單元格中提取文本。 –