当前位置:网站首页>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
边栏推荐
- X86 assembly syntax: at & T and Intel
- Range of numbers (dichotomous classic template topic)
- MySQL query uses \ g, column to row
- Markdown syntax support test
- 踩坑:nacos利用startup.cmd -m standalone启动错误
- Rog attack
- The list attribute in the entity is empty or null, and is set to an empty array
- Write the declaration of a function to return the reference of the array, and the array contains 10 string objects (notes)
- Cross platform packaging of QT packaging program
- uni使用的一些坑
猜你喜欢

Issue 36 summary of atcoder beginer contest 248

弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化

‘EddiesObservations‘ object has no attribute ‘filled‘

deep learning object detection

Three methods of list rendering

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

Differences between sea level anatomy and sea surface height anatomy

Arithmetic and logical operations

STL learning notes 0x0001 (container classification)

Cross platform packaging of QT packaging program
随机推荐
Utf8 to STD: string and STD: string to utf8
Differences between sea level anatomy and sea surface height anatomy
【华为机试】考试得分总数(如何处理答错的情况?回溯一次,代表答错一题)
Golang implements Ping connectivity detection case through exec module
‘EddiesObservations‘ object has no attribute ‘filled‘
Duplicate key update in MySQL
catkin_ What did package do
基于ssm 包包商城系统
poi生成excel,插入图片
50 SQL exercises, answers and detailed analysis
对象转map
The address value indicated by the pointer and the value of the object indicated by the pointer (learning notes)
Summary of redis classic interview questions 2022
Frequently asked interview questions - 2 (computer network)
Some pits used by uni
C语言——恶搞关机小程序
deep learning object detection
Similarities and differences between vector and array (notes)
STL learning notes 0x0001 (container classification)
[no title] Click the classification jump page to display the details