当前位置:网站首页>excel获取两列数据的差异数据
excel获取两列数据的差异数据
2022-04-23 05:39:00 【fusheng-fate】
背景
在日常工作中难免会遇到要处理excel数据的问题,比如 找出excel两列数据中 A列中存在B列中不存在的数据,B列中存在A列中不存在的数据。当数据量比较小的时候使用excel自带工具还比较方便,但是当数据量比较大时,处理就没有那么简单了,这时可以使用python操作,不需要搭建多复杂的环境,只需要引入python操作excel的两个库就行了。
pip install xlrd --user
pip install xlwt --user
import xlrd
import xlwt
def handle(list1, list2):
a = list(set(list1).difference(list2))
return a
class ExcelHandle:
def __init__(self, file_path, sheet_index):
# 拿到excel文件
self.rd_book = xlrd.open_workbook(file_path)
# 拿到excel中的某个sheet
self.sheet = self.rd_book.sheet_by_index(sheet_index)
# 读取行头
self.rowHead = self.sheet.row_values(0)
# 拿到全部的行
self.totalRows = self.sheet.nrows
def get_cols_list(self):
# 定义一个数组,存储要处理的列数据
col_data = []
for j in range(self.totalRows - 1):
# 遍历得到每一行的数据,得到的是一个数组
col = self.sheet.row_values(j + 1)
col_data.append(col[0])
return col_data
if __name__ == '__main__':
filePath = r'C:Users\fusheng-fate\Desktop\read_excel.xls'
self1 = ExcelHandle(filePath, 0)
sheet1_list = ExcelHandle.get_cols_list(self1)
self2 = ExcelHandle(filePath, 1)
sheet2_list = ExcelHandle.get_cols_list(self2)
a_b = handle(sheet1_list, sheet2_list)
b_a = handle(sheet2_list, sheet1_list)
# 把拿到的写入到文件中
work_book = xlwt.Workbook()
# 新增sheet
worksheet = work_book.add_sheet("差异")
# 表头
worksheet.write(0, 0, "1存在2不存在")
worksheet.write(0, 1, "2存在1不存在")
for i in range(len(a_b)):
worksheet.write(i+1, 0, a_b[i])
for i in range(len(b_a)):
worksheet.write(i+1, 1, b_a[i])
work_book.save(r'C:Users\fusheng-fate\Desktop\handle.xls')
结语
不太完美的是,使用xlwt这个库,在新增一个sheet,向这个sheet插入数据后会覆盖掉原有的sheet,所有把处理后的数据写入到了新的文件中,本意是想将处理后的数据放入到原有的excel中的。
版权声明
本文为[fusheng-fate]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lgb1997/article/details/112417558
边栏推荐
- The role of brackets' [] 'in the parameter writing method in MDN documents
- Introduction to qqueue
- 世界与个人发展
- Phlli in a VM node
- 弘玑|数字化时代下,HR如何进行自我变革和组织变革?
- Find the number of "blocks" in the matrix (BFS)
- Insert picture in freemark
- 第36期《AtCoder Beginner Contest 248 打比赛总结》
- 基于ssm 包包商城系统
- Three methods of list rendering
猜你喜欢

Traversal array, object parent-child communication props / $emit

Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios

Flutter 新一代圖形渲染器 Impeller

Isosceles triangle - the 9th Lanqiao provincial competition - group C

AcWing 836. Merge set (merge set)

STL learning notes 0x0001 (container classification)
![[untitled] Notepad content writing area](/img/0a/4a3636025c3e0441f45c99e3c67b67.png)
[untitled] Notepad content writing area

Interview Basics

uni使用的一些坑

Deep learning object detection
随机推荐
Frequently asked interview questions - 2 (computer network)
JSON.
POI generates excel and inserts pictures
OSI层常用协议
MDN文档里面入参写法中括号‘[]‘的作用
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
Shell instruction learning 1
Duplicate key update in MySQL
Differences between sea level anatomy and sea surface height anatomy
deep learning object detection
Map object map get(key)
Membarrier (personal learning and understanding)
实体中list属性为空或者null,设置为空数组
Several examples of pointer transfer, parameter transfer, value transfer, etc
idea插件---背景播放歌曲
Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
[machine learning] scikit learn introduction
Fast application fuzzy search
Introduction to data security -- detailed explanation of database audit system