我有一個包含一堆值的列表。我首先試圖找到帶有'MB'的項目,然後總結這些值。例如第一個「文字:u'3 MB」。我想要得到這個項目並且總結出在這種情況下將是3的值,並且對於具有「MB」的下一個項目在內部做同樣的處理。在列表中搜索並總結
代碼(到目前爲止):
#!/usr/bin/python
import xlrd
xl_workbook = xlrd.open_workbook("E:\usage.xls")
sheet_names = xl_workbook.sheet_names()
print('Sheet Names', sheet_names)
xl_sheet = xl_workbook.sheet_by_index(0)
print ('Sheet name: %s' % xl_sheet.name)
liste=[]
sum=0
num_cols = xl_sheet.ncols # Number of columns
for row_idx in range(0, xl_sheet.nrows): # Iterate through rows
for col_idx in range(0, num_cols): # Iterate through columns
cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col
liste.append(cell_obj)
#print ('cell_obj: [%s]' % (cell_obj))
for item in liste:
if "MB" in item:
print item
我收到此錯誤:
if "MB" in item:
TypeError: argument of type 'Cell' is not iterable
列表(包含):
[xldate:42340.671805555554, text:u'3 MB', empty:'', number:0.0, xldate:42340.5, text:u'12 MB', empty:'', number:0.0, xldate:42340.42820601852, text:u'10 MB', empty:'', number:0.0, xldate:42339.81946759259, text:u'8 MB', empty:'', number:0.0, xldate:42339.55652777778, text:u'6 MB', empty:'', number:0.0, xldate:42339.35625, text:u'10 MB', empty:'', number:0.0, empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'Totalt:', number:1.01]
內容'liste'無效語法 – poke