from xlrd import open_workbook
book = open_workbook('simple.xls',on_demand=True)
for name in book.sheet_names():
if name.endswith('2'):
sheet = book.sheet_by_name(name)
# Attempt to find a matching row (search the first column for 'john')
rowIndex = -1
for cell in sheet.col(0): #
if 'john' in cell.value:
break
# If we found the row, print it
if row != -1:
cells = sheet.row(row)
for cell in cells:
print cell.value
book.unload_sheet(name)
file_name = # path to file + file name
sheet = # sheet name or sheet number or list of sheet numbers and names
import pandas as pd
df = pd.read_excel(io=file_name, sheet_name=sheet)
print(df.head(5)) # print first 5 rows of the dataframe
试试the xlrd library。
[编辑]-从您的评论中我可以看到,下面的片段可能会起到作用。我在这里假设您只是在一个列中搜索单词“john”,但是您可以添加更多或使其成为一个更通用的函数。
这不像打开纯文本文件那么简单,而且需要某种外部模块,因为没有内置的模块来完成此操作。以下是一些选项:
http://www.python-excel.org/
如果可能,可以考虑将excel电子表格导出为CSV文件,然后使用内置的python CSV模块读取它:
http://docs.python.org/library/csv.html
编辑:
在较新版本的pandas中,可以将图纸名称作为参数传递。
查看文档中有关如何传递
sheet_name
:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html的示例
旧版本:} package 。。。。
你也可以使用^{
使用包含多个工作表的excel文件时,可以使用:
df.head()
将打印Excel文件的前5行如果使用Excel文件处理单个工作表,只需使用:
相关问题 更多 >
编程相关推荐