读取非结构化的xls文件
我有一个.xls文件,这个文件里有一列数据,总共有2000行。
我想要逐行查看这个文件,并打印出那些以“cheap”开头的数据点。不过,下面的代码没有成功。
求助!
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'
1 个回答
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