如何使用Python读取Excel文件中的特定行

5

我想使用Python从Excel_file1的Sheet1中读取第7行的单元格数据,可以帮忙吗?

2个回答

10

首先安装xlrd

pip install xlrd

然后打开Python文件并

import xlrd 

# Give the location of the file 
loc = ("path of file") 

# To open Workbook 
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 

print(sheet.row_values(7)) 

位置是相对路径而不是绝对路径。

要了解更多关于xlrd及其使用的信息,请访问https://xlrd.readthedocs.io/en/latest/

愉快的编码。


7

您也可以使用pd.read_excel函数,该函数属于pandas库:

您需要先安装pandasxlrd

import pandas as pd
import xlrd
df = pd.read_excel('abc.xlsx', sheet_name='Sheet1')

现在,您可以使用iloc筛选数据框以获取任何特定行。
df.iloc[6] ## This will give you 7th row

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接