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)'不会立即给你提供字符串。阅读您的图书馆的文档,并了解如何从单元格中提取文本。 –