网站首页 > 基础教程 正文
使用xlrd模块,可以从电子表格中检索信息。例如,可以在Python中读取、写入或修改数据。此外,用户可能需要浏览各种工作表并根据某些条件检索数据,或者修改某些行和列并执行大量工作。
xlrd模块用于从电子表格中提取数据。
安装xlrd模块的命令:
pip install xlrd
输入文件:
代码1:
# Reading an excel file using 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)
# For row 0 and column 0
sheet.cell_value(0, 0)
输出:
'NAME'
代码2:提取行数
# Program to extract number
# of rows using Python
import xlrd
# Give the location of the file
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
# Extracting number of rows
print(sheet.nrows)
输出:
4
代码3:提取列数
# Program to extract number of
# columns in Python
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
# Extracting number of columns
print(sheet.ncols)
输出:
3
代码4:提取所有列名称
# Program extracting all columns
# name in Python
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
for i in range(sheet.ncols):
print(sheet.cell_value(0, i))
输出:
NAME
SEMESTER
ROLL NO
代码5:提取第一列
# Program extracting first column
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
for i in range(sheet.nrows):
print(sheet.cell_value(i, 0))
输出:
NAME
ALEX
CLAY
JUSTIN
代码6:提取特定的行值
# Program to extract a particular row value
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
print(sheet.row_values(1))
输出:
['ALEX', 4.0, 2011272.0]]
猜你喜欢
- 2024-11-15 自动化测试学习:使用python库Paramiko实现远程服务器上传和下载
- 2024-11-15 PyPDF2是python的处理PDF文件的基本操作介绍
- 2024-11-15 RAG实战篇:精准判断用户查询意图,自动选择最佳处理方案
- 2024-11-15 03 Python数据类型转换(python数据类型转换函数)
- 2024-11-15 Pytest丨如何使用全功能的Python测试框架?小白必看
- 2024-11-15 Python自带单元测试框架UnitTest,如何生成独立的测试报告?
- 2024-11-15 新手常见的python报错及解决方案(新手常见的python报错及解决方案有哪些)
- 2024-11-15 使用Python 模块 轻松集成功能(python用什么集成开发环境)
- 2024-11-15 Python学习教程(二)(“python教程”)
- 2024-11-15 python库的属性方法帮助以及ascII
- 06-18单例模式谁都会,破坏单例模式听说过吗?
- 06-18Objective-c单例模式的正确写法「藏」
- 06-18单例模式介绍(单例模式都有哪些)
- 06-18前端设计-单例模式在实战中的应用技巧
- 06-18PHP之单例模式(php单例模式连接数据库)
- 06-18设计模式:单例模式及C及C++实现示例
- 06-18python的单例模式(单例 python)
- 06-18你认为最简单的单例模式,东西还挺多
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)