当前位置:网站首页>Excel obtains the difference data of two columns of data
Excel obtains the difference data of two columns of data
2022-04-23 05:44:00 【fusheng-fate】
background
In daily work, we will inevitably encounter problems to deal with excel Data problems , such as find excel In two columns of data A Column exists B Data that does not exist in the column ,B Column exists A Data that does not exist in the column . Use when the amount of data is small excel It's convenient to bring your own tools , But when the amount of data is large , It's not that easy to deal with , You can use python operation , There is no need to build a complex environment , Just import python operation excel Just two libraries .
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):
# Get excel file
self.rd_book = xlrd.open_workbook(file_path)
# Get excel One of the sheet
self.sheet = self.rd_book.sheet_by_index(sheet_index)
# Read line header
self.rowHead = self.sheet.row_values(0)
# Get all the lines
self.totalRows = self.sheet.nrows
def get_cols_list(self):
# Define an array , Store the column data to be processed
col_data = []
for j in range(self.totalRows - 1):
# Traverse to get the data of each row , What we get is an array
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)
# Write what you get into the file
work_book = xlwt.Workbook()
# newly added sheet
worksheet = work_book.add_sheet(" differences ")
# Header
worksheet.write(0, 0, "1 There is 2 non-existent ")
worksheet.write(0, 1, "2 There is 1 non-existent ")
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')
Conclusion
What's not perfect is , Use xlwt This library , Add a new sheet, To this sheet After inserting data, the original... Will be overwritten sheet, All the processed data is written into a new file , The original intention is to put the processed data into the original excel Medium .
版权声明
本文为[fusheng-fate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230539252753.html
边栏推荐
- Hotkeys, interface visualization configuration (interface interaction)
- solidity合约DOS攻击
- Isosceles triangle - the 9th Lanqiao provincial competition - group C
- Pavlov and hobbies
- Duplicate key update in MySQL
- What financial products will benefit during May Day?
- 弘玑|数字化时代下,HR如何进行自我变革和组织变革?
- idea插件---背景播放歌曲
- 合约锁仓漏洞
- 2-软件设计原则
猜你喜欢

弘玑|数字化时代下,HR如何进行自我变革和组织变革?

QT drawpixmap and DrawImage blur problem

mysql实现主从复制/主从同步

Find the number of "blocks" in the matrix (BFS)

Arithmetic and logical operations

第36期《AtCoder Beginner Contest 248 打比赛总结》

Pol / select / EPO

‘EddiesObservations‘ object has no attribute ‘filled‘

Generation of straightening body in 3D slicer

3d slicer中拉直体的生成
随机推荐
solidity合约DOS攻击
Summary of redis classic interview questions 2022
QT displays the specified position and size of the picture
Traversal array, object parent-child communication props / $emit
Common status codes
js数字大写方法
巴普洛夫与兴趣爱好
freemark中插入图片
acwing854. Floyd finds the shortest path
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
五一劳动节期间什么理财产品会有收益?
IPI interrupt
第36期《AtCoder Beginner Contest 248 打比赛总结》
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
DWSurvey是一个开源的调查问卷系统。解决无法运行问题,修改bug。
The role of brackets' [] 'in the parameter writing method in MDN documents
Relative reference and absolute reference of Excel
Frequently asked interview questions - 1 (non technical)
poi生成excel,插入图片
catkin_package到底干了什么