当前位置:网站首页>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
边栏推荐
- The address value indicated by the pointer and the value of the object indicated by the pointer (learning notes)
- Insert picture in freemark
- 50 SQL exercises, answers and detailed analysis
- Deep learning object detection
- 提升Facebook触及率和互动率攻略 | 智能客服帮您抓住用户的心
- AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
- Usage and difference of shellexecute, shellexecuteex and winexec in QT
- Parameter analysis of open3d material setting
- Pavlov and hobbies
- Linear sieve method (prime sieve)
猜你喜欢
Hongji | how does HR carry out self change and organizational change in the digital era?
Duplicate key update in MySQL
基于ssm 包包商城系统
Isosceles triangle - the 9th Lanqiao provincial competition - group C
On the use of constant pointer and pointer constant -- exercise (record)
QT displays the specified position and size of the picture
C language - Spoof shutdown applet
‘EddiesObservations‘ object has no attribute ‘filled‘
STL learning notes 0x0001 (container classification)
Data mining -- understanding data
随机推荐
open3d材质设置参数分析
世界与个人发展
Formal parameters, local variables and local static variables
SQL语句简单优化
Summary of redis classic interview questions 2022
JS number capitalization method
IPI interrupt
巴普洛夫与兴趣爱好
poi导出excel,行相同数据自动合并单元格
STD:: String implements split
Batch import of orange single micro service
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
Generation of straightening body in 3D slicer
Utf8 to STD: string and STD: string to utf8
2 - software design principles
catkin_ What did package do
The 8th Blue Bridge Cup 2017 - frog jumping cup
转置卷积(Transposed Convolution)
Idea plug-in --- playing songs in the background
Cmake basic tutorial (39) pkgconfig