python+selenium+new——xlrd库——读取excel文件——xls结尾为示例 ——数据格式

from datetime import date, datetime

import xlrd

# 单元格类型:  0. empty(空的),1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank(空白表格)

book = xlrd.open_workbook("C:\\Users\\del\\Desktop\\Book2.xls")

print(book.nsheets)          #返回sheet的数量,此处返回 3

print(book.sheet_names())   #返回所有sheet名称;[‘党费‘, ‘Sheet2‘, ‘Sheet3‘]

sheet_name = book.sheet_names()[0]  # 获取指定索引的sheet的名字
print(sheet_name)  # Sheet1

# sheet0 = book.sheet_by_index(0)  #通过sheet索引获取sheet对象

sheet0 = book.sheet_by_name("党费")

print(sheet0.nrows)  # 总行数:2
print(sheet0.ncols)  # 总列数:6

print(sheet0.row_values(0))  # 获取第一行数据:[‘身份证号‘, ‘姓名‘, ‘学号‘, ‘缴费项目‘, ‘缴费金额‘, ‘出生日期‘]
print(sheet0.row_values(1))  # 获取第二行数据:[‘33038119890412221X‘, ‘潘颂哲‘, ‘33038119890412221X‘, ‘2020年2月份党费‘, 14.0, 32610.0]

print(sheet0.col_values(0))  # 获取第一列的数据:[‘身份证号‘, ‘33038119890412221X‘]
print(sheet0.col_values(1))  # 获取第二列的数据:[‘姓名‘, ‘潘颂哲‘]
print(sheet0.col_values(2))  # 获取第三列的数据:[‘学号‘, ‘33038119890412221X‘]
print(sheet0.col_values(3))  # 获取第四列的数据:[‘缴费项目‘, ‘2020年2月份党费‘]
print(sheet0.col_values(4))  # 获取第五列的数据:[‘缴费金额‘, 14.0]
print(sheet0.col_values(5))  # 获取第六列的数据:[‘出生日期‘, 32610.0]

# 通过cell的位置坐标获取指定cell的值:

print(sheet0.cell_value(0, 0))  # 获取第一行第一列的数据:身份证号
print(sheet0.cell_value(0, 1))  # 获取第一行第二列的数据:姓名

print(sheet0.cell_value(1, 4))  # 获取第二行第五列的数据:14.0
print(sheet0.cell(1,4).value)  # 获取第二行第五列的数据:14.0

print(sheet0.cell(1,5).value)  # 获取第二行第五列的数据:32610.0
#----------------------------------------

# 数据类型  0 empty,

#   1 string,

#   2 number,

#   3 date,

#   4 boolean,

#   5 error

#------------------------------------------------

print(sheet0.cell(1,4).ctype)  #返回2 ,说明是number类型

print(type(sheet0.cell(1,4).value))  #返回<class ‘float‘>

print(int(sheet0.cell(1,4).value))   #返回14
print(type(int(sheet0.cell(1,4).value)))  #返回<class ‘int‘>

#-----------------------------------------------------------------------------

print(sheet0.cell(1,5).ctype)  #返回3,说明是date类型

print(xlrd.xldate_as_tuple(sheet0.cell_value(1,5),book.datemode))   #返回(1989, 4, 12, 0, 0, 0)

date_value = xlrd.xldate_as_tuple(sheet0.cell_value(1,5),book.datemode)

print(date(*date_value[:3]))   #返回 1989-04-12

print(date(*date_value[:3]).strftime(‘%Y/%m/%d‘))   #返回:1989/04/12

print(date(*date_value[:3]).strftime(‘%Y-%m-%d‘))   #返回:1989-04-12

#--------------------------------------------------------------------------------

print(sheet0.row(0))      #获取指定行,返回cell对象的列表   同时展示行中各cell对象的数据类型[text:‘身份证号‘, text:‘姓名‘, text:‘学号‘, text:‘缴费项目‘, text:‘缴费金额‘, text:‘出生日期‘]

print(sheet0.row_values(0) )  #获取指定行,返回列表[‘身份证号‘, ‘姓名‘, ‘学号‘, ‘缴费项目‘, ‘缴费金额‘, ‘出生日期‘]

print(sheet0.row(1)) #[text:‘33038119890412221X‘, text:‘潘颂哲‘, text:‘33038119890412221X‘, text:‘2020年2月份党费‘, number:14.0, xldate:32610.0]

print(sheet0.col(0))          #获取指定列,返回cell对象的列表   同时展示列中各cell对象的数据类型 [text:‘身份证号‘, text:‘33038119890412221X‘]

print(sheet0.col_values(0) )  #获取指定列,返回列表   列中的内容[‘身份证号‘, ‘33038119890412221X‘]

执行结果:

3
[‘党费‘, ‘Sheet2‘, ‘Sheet3‘]
党费
2
6
[‘身份证号‘, ‘姓名‘, ‘学号‘, ‘缴费项目‘, ‘缴费金额‘, ‘出生日期‘]
[‘33038119890412221X‘, ‘潘颂哲‘, ‘33038119890412221X‘, ‘2020年2月份党费‘, 14.0, 32610.0]
[‘身份证号‘, ‘33038119890412221X‘]
[‘姓名‘, ‘潘颂哲‘]
[‘学号‘, ‘33038119890412221X‘]
[‘缴费项目‘, ‘2020年2月份党费‘]
[‘缴费金额‘, 14.0]
[‘出生日期‘, 32610.0]
身份证号
姓名
14.0
14.0
32610.0
2
<class ‘float‘>
14
<class ‘int‘>
3
(1989, 4, 12, 0, 0, 0)
1989-04-12
1989/04/12
1989-04-12
[text:‘身份证号‘, text:‘姓名‘, text:‘学号‘, text:‘缴费项目‘, text:‘缴费金额‘, text:‘出生日期‘]
[‘身份证号‘, ‘姓名‘, ‘学号‘, ‘缴费项目‘, ‘缴费金额‘, ‘出生日期‘]
[text:‘33038119890412221X‘, text:‘潘颂哲‘, text:‘33038119890412221X‘, text:‘2020年2月份党费‘, number:14.0, xldate:32610.0]
[text:‘身份证号‘, text:‘33038119890412221X‘]
[‘身份证号‘, ‘33038119890412221X‘]

原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12323152.html

时间: 2024-08-01 06:28:50

python+selenium+new——xlrd库——读取excel文件——xls结尾为示例 ——数据格式的相关文章

python+selenium+new——xlrd库——读取excel文件——xlsx结尾为示例

pip  install  xlrd           #j导入这个库 import xlrd book = xlrd.open_workbook("C:\\Users\\del\\Desktop\\Book1.xlsx") sheet_name = book.sheet_names()[0] #获取指定索引的sheet的名字 print(sheet_name) #Sheet1 sheet0 = book.sheet_by_index(0) #通过sheet索引获取sheet对象 p

人生苦短_我用Python_openpyxl库读取Excel文件数据_008

上图为读取的目标文件--------------------------------------------------------------------------------- # coding=utf-8 ''': 第一步:最简单的读取文件Demo,读取文件中的某个值,和写入某个单元格的值 Excel wordbook 工作簿 ->>确定sheet表单 --->cell 单元格 pip install openpyxl ''' # 终极目标 读取successed # from

好记性不如烂笔头8-JAVA读取EXCEL文件

使用poi读取EXCEL的内容 在很多的场合,需要读取EXCEL文件.简单的示例. 需要引入第三方jar包:poi_3.6.jar package com.daily; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; impor

python第三方库学习之xlrd读取Excel文件

因为经常会涉及到从Excel表中导数据,所以就学习了python的xlrd来读取excel中的数据. 1.xlrd的安装 xlrd是python的第三方库,所以是需要自己安装的,可以在python的官网http://pypi.python.org/pypi/xlrd下载该模块来安装,也可以通过其他手段,比如easy_install或者pip啥的,我已经安装好pip所以就用最懒的方式来安装了pip install xlrd来安装. 2.分析excel文件的层级对象 要读取excel的数据,就要了解

Python 读取 excel 文件

现在大部分数据都是存储在excel中或直接调取数据库,作为刚刚自学Python的小白来说怎么读取文件还是比较懵的,现在对Python读取excel文件进行了一些整理: #coding=utf-8 #cmd中进行安装xlrd库 pip install xlrd import xlrd #文件路径,要用/而不是\ file_path = r'C:/Users/mingli.zhao/Desktop/七天.xlsx' #中文转码 #file_path = file_path.decode('utf-8

python读取excel文件(xrld模块)

Python读取excel文件 一.python  xlrd模块 安装 mac 下安装python  xlrd模块 http://www.crifan.com/python_read_excel_xls_file_xlrd/comment-page-1/ python setup.py install 在mac 下出现的错误是 http://stackoverflow.com/questions/18199853/error-could-not-create-library-python-2-7

python读取excel文件

一.xlrd的说明 xlrd是专门用来在python中读取excel文档的模块,使用前需要安装. 可以到这https://pypi.python.org/pypi/xlrd进行下载tar.gz文件,然后解压缩安装,在cmd命令窗口中切换到解压后的文件夹中,使用 python setup.py install 进行安装. 方法二. 使用pip进行安装 pip install xlrd 二.使用介绍 1导入模块 import xlrd 2 打开excel文件 data = xlrd.open_wor

python 读取 excel文件

python读取excel文件的链接都是从这里获取的: http://blog.csdn.net/longshen747/article/details/17194259 http://www.cnblogs.com/yanzhi123/archive/2012/04/16/2452214.html 上个示例的代码: import xml.etree.ElementTree as ETimport xlwtimport os path = "D:/Cai_Bishe/xml/"prin

xlrd---Python中读取excel文件的利器

xlrd是Python中常用于解析excel文件的模块,提供了非常简单易用的API来完成相关操作. 相应地,xlwt常用于向excel文件中写入内容. xlrd的常用使用方法如下: import xlrd book = xlrd.open_workbook("speechs.xlsx", "utf8") sheet = book.sheet_by_name(u'机器地址') # 通过名字来查找对应的sheet rows = sheet.nrows # 读取行数 fo