2017-04-27 218 views
0

我想从我的exceltest.xlsx文件中提取整数。整数在文件的A列(第2行= 20,第3行= 30,第4行= 40)。当我运行下面的代码时,我在list1中得到以下内容:[number:20.0,number:30.0,number:40.0]。我怎样才能让它返回[20,30,40]呢?我正在尝试将list1写入excel文件,当list1包含术语“数字”时它不起作用。我已经成功地将我在我的代码中定义的列表和元组写入Excel,但在从一个Excel文件中将整数写入另一个Excel文件的情况下,我正在挣扎。谢谢。在python中使用xlrd从excel中获取整数作为我的列表,但整数返回为(number:integer)

book = open_workbook("exceltest.xlsx") 
sheet = book.sheet_by_index(0) 
list1 = [] 

for row in range(1,4): 
    list1.append(sheet.cell(row,0)) 

回答

0

更换

list1.append(sheet.cell(row, 0)) 

list1.append(sheet.cell(row, 0).value) 

你也可以考虑像这样的东西取代了 'for' 循环。

allrows = [sheet.row_values(i)[0] for i in range(sheet.nrows) if sheet.row_values(i)[0]] 
相关问题