Python Excel数据处理之xlrd/xlwt/xlutils模块怎么使用

常规的excel数据处理中,就是对excel数据文件的读/写/文件对象操作。

通过对应的python非标准库xlrd/xlwt/xlutils,来实现具体的数据处理业务逻辑。

在复杂的Excel业务数据处理中,三兄弟扮演的角色缺一不可。今天我们的内容是关于如何采用xlrd/xlwt/xlutils三个模块来实现数据处理。

1、模块说明

使用该三个模块来处理Excel数据最好的地方就是他们和Excel文件对象对应的数据处理概念是一样的,能更好的便于我们理解数据对象。

首先,这三个模块都是python的非标准库,可以选择pip的方式来进行安装。

立即学习“Python免费学习笔记(深入)”;

pip install xlrdpip install xlwtpip install xlutils

登录后复制

下面是我们为演示数据处理的过程准备的源数据内容,只是用于测试。

Python Excel数据处理之xlrd/xlwt/xlutils模块怎么使用

xlrd:用于读取Excle数据文件将返回的数据对象放到内存中,然后查询数据文件对象的相关信息。

xlwt:用于在内存中生成新的数据文件对象,处理完成后写入到Excel数据文件中。

xlutils:主要的作用就是copy新的文件对象,在新的数据对象中完成数据处理操作。

将xlrd/xlwt/xlutils三个模块分别都导入到待开发的代码块中提供支持。

# Importing the xlrd module.import xlrd as read# Importing the xlwt module.import xlwt as write# Copying the contents of the original workbook into a new workbook.from xlutils.copy import copy

登录后复制

2、xlrd处理

# Opening the workbook and assigning it to the variable `work_book`.work_book = read.open_workbook('D:/test-data-work/test.xls')# Assigning the sheet named 'Sheet1' to the variable `sheet`.sheet = work_book.sheet_by_name('Sheet1')# `row = sheet.nrows` is assigning the number of rows in the sheet to the variable `row`.row = sheet.nrows# `col = sheet.ncols` is assigning the number of columns in the sheet to the variable `col`.col = sheet.ncolsprint('Sheet1工作表有:{0}行,{1}列'.format(str(row), str(col)))# Sheet1工作表有:23行,5列

登录后复制

下面是三种常用的sheet对象的数据遍历方式,分别是按行/列的方式进行数据遍历。

for a in sheet.get_rows():    print(a)# [text:'姓名', text:'年龄', text:'班级', text:'成绩', text:'表现']# [text:'Python 集中营', number:20.0, number:1210.0, number:90.0, text:'A']# [text:'Python 集中营', number:21.0, number:1211.0, number:91.0, text:'A']# [text:'Python 集中营', number:22.0, number:1212.0, number:92.0, text:'A']# [text:'Python 集中营', number:23.0, number:1213.0, number:93.0, text:'A']# [text:'Python 集中营', number:24.0, number:1214.0, number:94.0, text:'A']# [text:'Python 集中营', number:25.0, number:1215.0, number:95.0, text:'A']# [text:'Python 集中营', number:26.0, number:1216.0, number:96.0, text:'A']# [text:'Python 集中营', number:27.0, number:1217.0, number:97.0, text:'A']# [text:'Python 集中营', number:28.0, number:1218.0, number:98.0, text:'A']# [text:'Python 集中营', number:29.0, number:1219.0, number:99.0, text:'A']# [text:'Python 集中营', number:30.0, number:1220.0, number:100.0, text:'A']# [text:'Python 集中营', number:31.0, number:1221.0, number:101.0, text:'A']# [text:'Python 集中营', number:32.0, number:1222.0, number:102.0, text:'A']# [text:'Python 集中营', number:33.0, number:1223.0, number:103.0, text:'A']# [text:'Python 集中营', number:34.0, number:1224.0, number:104.0, text:'A']# [text:'Python 集中营', number:35.0, number:1225.0, number:105.0, text:'A']# [text:'Python 集中营', number:36.0, number:1226.0, number:106.0, text:'A']# [text:'Python 集中营', number:37.0, number:1227.0, number:107.0, text:'A']# [text:'Python 集中营', number:38.0, number:1228.0, number:108.0, text:'A']# [text:'Python 集中营', number:39.0, number:1229.0, number:109.0, text:'A']# [text:'Python 集中营', number:40.0, number:1230.0, number:110.0, text:'A']# [text:'Python 集中营', number:41.0, number:1231.0, number:111.0, text:'A']for b in range(row):    print(sheet.row_values(b))# ['姓名', '年龄', '班级', '成绩', '表现']# ['Python 集中营', 20.0, 1210.0, 90.0, 'A']# ['Python 集中营', 21.0, 1211.0, 91.0, 'A']# ['Python 集中营', 22.0, 1212.0, 92.0, 'A']# ['Python 集中营', 23.0, 1213.0, 93.0, 'A']# ['Python 集中营', 24.0, 1214.0, 94.0, 'A']# ['Python 集中营', 25.0, 1215.0, 95.0, 'A']# ['Python 集中营', 26.0, 1216.0, 96.0, 'A']# ['Python 集中营', 27.0, 1217.0, 97.0, 'A']# ['Python 集中营', 28.0, 1218.0, 98.0, 'A']# ['Python 集中营', 29.0, 1219.0, 99.0, 'A']# ['Python 集中营', 30.0, 1220.0, 100.0, 'A']# ['Python 集中营', 31.0, 1221.0, 101.0, 'A']# ['Python 集中营', 32.0, 1222.0, 102.0, 'A']# ['Python 集中营', 33.0, 1223.0, 103.0, 'A']# ['Python 集中营', 34.0, 1224.0, 104.0, 'A']# ['Python 集中营', 35.0, 1225.0, 105.0, 'A']# ['Python 集中营', 36.0, 1226.0, 106.0, 'A']# ['Python 集中营', 37.0, 1227.0, 107.0, 'A']# ['Python 集中营', 38.0, 1228.0, 108.0, 'A']# ['Python 集中营', 39.0, 1229.0, 109.0, 'A']# ['Python 集中营', 40.0, 1230.0, 110.0, 'A']# ['Python 集中营', 41.0, 1231.0, 111.0, 'A']for c in range(col):    print(sheet.col_values(c))# ['姓名', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营']# ['年龄', 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0]# ['班级', 1210.0, 1211.0, 1212.0, 1213.0, 1214.0, 1215.0, 1216.0, 1217.0, 1218.0, 1219.0, 1220.0, 1221.0, 1222.0, 1223.0, 1224.0, 1225.0, 1226.0, 1227.0, 1228.0, 1229.0, 1230.0, 1231.0]# ['成绩', 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0]# ['表现', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']

登录后复制

3、xlwt处理

# Creating a new workbook.work_book_2 = write.Workbook()# Creating a new sheet named 'Sheet4' in the workbook.sheet_2 = work_book_2.add_sheet('Sheet4')list = [    ['姓名', '年龄', '班级', '成绩'],    ['张三', '20', '1210', '89'],    ['李四', '21', '1211', '90'],    ['王五', '22', '1212', '91'],]for row_index in range(4):    for col_index in range(4):        sheet_2.write(row_index, col_index, list[row_index][col_index])        col_index += 1    row_index += 1# Saving the workbook to the specified location.work_book_2.save('D:/test-data-work/test2.xls')

登录后复制

4、xlutils处理

# Opening the workbook and assigning it to the variable `work_book_3`.work_book_3 = read.open_workbook('D:/test-data-work/test.xls')# Copying the contents of the original workbook into a new workbook.work_book_3_copy = copy(work_book_3)# Saving the contents of the original workbook into a new workbook.work_book_3_copy.save('D:/test-data-work/test3.xls')

登录后复制

以上就是Python Excel数据处理之xlrd/xlwt/xlutils模块怎么使用的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2235323.html

(0)
上一篇 2025年2月26日 17:58:58
下一篇 2025年2月26日 17:59:33

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 怎么用python代码实现图片去摩尔纹

    1、前言 当感光元件像素的空间频率与影像中条纹的空间频率接近时,可能产生一种新的波浪形的干扰图案,即所谓的摩尔纹。传感器的网格状纹理构成了一个这样的图案。若图案中的细条状结构与传感器的结构呈小角度交叉,则会在图像中产生明显的干扰效应。这种现…

    2025年2月26日
    000
  • Python怎么使用ClickHouse

    clickhouse是近年来备受关注的开源列式数据库(dbms),主要用于数据联机分析(olap)领域,于2016年开源。目前国内社区火热,各个大厂纷纷跟进大规模使用。 今日头条,内部用ClickHouse来做用户行为分析,内部一共几千个C…

    2025年2月26日 编程技术
    200
  • Python递归下降Parser怎么实现

    1. 算术运算表达式求值 要解析这类文本,需要另外一种特定的语法规则。我们这里介绍可以表示上下文无关文法(context free grammer)的语法规则巴科斯范式(bnf)和扩展巴科斯范式(ebnf)。从小到一个算术运算表达式,到大到…

    2025年2月26日
    200
  • python查找与排序算法实例代码分析

    查找 二分查找 二分搜索是一种在有序数组中查找某一特定元素的搜索算法。搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一…

    2025年2月26日 编程技术
    200
  • Python自动化测试利器selenium怎么使用

    1 自动化测试 自动化测试指软件测试的自动化,在预设状态下运行应用程序或者系统,预设条件包括正常和异常,最后评估运行结果。将人为驱动的测试行为转化为机器执行的过程。 自动化测试包括UI自动化,接口自动化,单元测试自动化。按照这个金字塔模型来…

    2025年2月26日
    200
  • 怎么使用Python开发自定义Web框架

    开发自定义Web框架 接收web服务器的动态资源请求,给web服务器提供处理动态资源请求的服务。根据请求资源路径的后缀名进行判断: 如果请求资源路径的后缀名是.html则是动态资源请求, 让web框架程序进行处理。 否则是静态资源请求,让w…

    2025年2月26日 编程技术
    200
  • Python sklearn怎么对文本数据进行特征化提取

    文本特征提取 作用:对文本数据进行特征化 (句子、短语、单词、字母)一般选用单词作为特征值 方法一:CountVectorizer sklearn.feature_extraction.text.CountVectorizer(stop_w…

    编程技术 2025年2月26日
    200
  • 如何使用python+Flask实现日志在web网页实时更新显示

    一、日志输出到文件 使用模块:logging 可以生成自定义等级日志,可以输出日志到指定路径日志等级:debug(调试日志) 1、封装日志输出方法() import logging as lgimport osclass logging_(…

    2025年2月26日 编程技术
    200
  • python如何实现自动化运维

    1、安装 pip install paramiko 2、导入模块 import paramiko 3、使用 def initSshClinet(): ”’ 初始化,SSH连接账号密码登录服务器 :return: ss…

    编程技术 2025年2月26日
    200
  • python中的np.random.permutation函数怎么使用

    一:函数介绍 np.random.permutation() 总体来说他是一个随机排列函数,就是将输入的数据进行随机排列,官方文档指出,此函数只能针对一维数据随机排列,对于多维数据只能对第一维度的数据进行随机排列。简而言之:np.rando…

    2025年2月26日 编程技术
    200

发表回复

登录后才能评论