【问题标题】:Read through an unstructured xls file通读一个非结构化的 xls 文件
【发布时间】:2011-06-01 22:46:57
【问题描述】:

我有一个 .xls 文件,其中包含一列 2,000 行。

我想遍历文件并打印出数据点 以“便宜”开头。但是,下面的代码不起作用。

救命!

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'

【问题讨论】:

    标签: python file xls


    【解决方案1】:

    它应该看起来像:

    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
    

    【讨论】:

    • 我收到以下错误 AttributeError: 'Cell' object has no attribute 'startswith'
    猜你喜欢
    • 2018-09-19
    • 1970-01-01
    • 2011-11-04
    • 2011-08-18
    • 1970-01-01
    • 2011-03-10
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多