本文概述
- Python3
- Python3
- Python3
- Python3
- Python3
- Python3
rd模块用于从电子表格中提取数据。
安装xlrd模块的命令:
pip install xlrd
输入文件:
文章图片
代码1:提取特定的单元格
Python3
# 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
print (sheet.cell_value( 0 , 0 ))
输出:
'NAME'
代码2:提取行数
Python3
# 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:提取列数
Python3
# 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:提取所有列名称
Python3
# 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:提取第一列
Python3
# 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:提取特定的行值
Python3
# 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]
【如何使用Python读取Excel文件()】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- 邮递员面试经验| 2020年软件工程实习(校外)
- 算法设计(找零钱问题介绍和详细解决方案|DP-7)
- Python探索相关性详细指南
- SQL中自然联接和内部联接之间的区别
- 高级算法(跳转指针算法原理介绍和实现)
- 转(Android-apt)
- Android使用代码设置Dialog的Style
- Android 反编译工具
- android:layout_weight属性的使用方法总结