当前位置:网站首页>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
边栏推荐
- World and personal development
- Insert picture in freemark
- solidity合约DOS攻击
- Qwebsocket communication
- Differences between sea level anatomy and sea surface height anatomy
- Range of numbers (dichotomous classic template topic)
- POI generates excel and inserts pictures
- Use of qwbengneview and qwebchannel.
- Sword finger offer II 022 The entry node of the link in the linked list
- STL function library
猜你喜欢
Common protocols of OSI layer
Breadth first search topics (BFS)
QT drawpixmap and DrawImage blur problem
Transposed convolution
Flutter 新一代圖形渲染器 Impeller
Excel sets row and column colors according to cell contents
Cross platform packaging of QT packaging program
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
QT displays the specified position and size of the picture
Pytorch deep learning practice_ 11 convolutional neural network
随机推荐
Map object map get(key)
Range of numbers (dichotomous classic template topic)
转置卷积(Transposed Convolution)
Rog attack
Formal parameters, local variables and local static variables
巴普洛夫与兴趣爱好
Hongji | how does HR carry out self change and organizational change in the digital era?
what is wifi6?
Solid contract DoS attack
Markdown syntax support test
idea插件---背景播放歌曲
‘EddiesObservations‘ object has no attribute ‘filled‘
Interview Basics
合约锁仓漏洞
solidity合约DOS攻击
SQL语句简单优化
Cross platform packaging of QT packaging program
Batch import of orange single micro service
Differences between sea level anatomy and sea surface height anatomy
【华为机试】考试得分总数(如何处理答错的情况?回溯一次,代表答错一题)