2011-06-01 68 views
1

我有一個.xls文件,其中包含一列2000行。通讀非結構化xls文件

我想遍歷文件並打印出以「便宜」開頭的數據點 。但是,下面的代碼不起作用。

幫助!

import xlrd 
wb = xlrd.open_workbook("file.xls") 

wb.sheet_names() 

sh = wb.sheet_by_index(0) 
lst = [sh] 

for item in lst: 
    print item.startswith("cheap") 

    Traceback (most recent call last): 
    File "C:\Python26\keywords.py", line 14, in <module> 
    print item.startswith("cheap") 
AttributeError: 'Sheet' object has no attribute 'startswith' 

回答

2

它應該看起來像:

import xlrd 
wb = xlrd.open_workbook("file.xls") 

wb.sheet_names() 

sh = wb.sheet_by_index(0) 

for item in sh.col(0): 
    value = unicode(item.value) 
    if value.startswith("cheap"): 
     print value 
+0

我收到以下錯誤AttributeError的: '細胞' 對象有沒有屬性 'startswith' – ATMathew 2011-06-01 22:59:55

+0

現在它的固定:) – virhilo 2011-06-01 23:04:36

+0

+1謝謝主席先生! – ATMathew 2011-06-01 23:07:14