首页 > 编程语言 >Python实现两个excel数据匹配

Python实现两个excel数据匹配

时间:2022-12-04 08:55:56浏览次数:58  
标签:index 匹配 target Python excel write index2 sheet index1

Python实现两个excel数据匹配

本文结合他人文章写成

参考文章:

https://blog.csdn.net/u010034713/article/details/110921270

https://www.cnblogs.com/puresoul/p/7520198.html

需求背景

表1有两列

img

表2包含表1不过缺少坐标字段

img

需要根据HID匹配两个表,把表1的坐标内容补充到表2

代码

import shutil
import sys
import xlwt
import xlrd
 
file1 = "C:\\Users\\admin\\Desktop\\新建文件夹\\match-excel\\表1.xls"
#打开表1
wb1 = xlrd.open_workbook(filename=file1)
# 表1要匹配的列索引
hid_index1 = 0
# 表1目标数据列索引
target_index1 = 1
# 表1的sheet
sheet1 = wb1.sheet_by_index(0)
# 表1的sheet的总行数
rowNum1 = sheet1.nrows
# 表1的sheet的总列数
colNum1 = sheet1.ncols
 
file2 = "C:\\Users\\admin\\Desktop\\新建文件夹\\match-excel\\表2.xls"
#打开表2
wb2 = xlrd.open_workbook(filename=file2)#打开文件
# 表2要匹配的列索引
hid_index2 = 0
# 表2目标数据列索引
target_index2 = 2
# 表2的sheet
sheet2 = wb2.sheet_by_index(0)#通过索引获取表格
# 表2的sheet的总行数
rowNum2 = sheet2.nrows
# 表2的sheet的总列数
colNum2 = sheet2.ncols
 
# xlwt准备生成一个新的文件
write_workbook = xlwt.Workbook()
write_sheet = write_workbook.add_sheet('sheet1',cell_overwrite_ok=True)
 
for index2 in range(0,rowNum2):
	for col_index in range(0,colNum2):
		# 遍历表2的每一行每一列,把对应的单元设置到新的文件中,即复制了表2的数据
		write_sheet.write(index2,col_index,sheet2.cell_value(index2,col_index))
		# 在遍历列过程中,如果碰到目标数据列索引.即需要补充的字段,则进行遍历表1,判断的id索引匹配
		if col_index == target_index2:
			for index1 in range(1,rowNum1):
				hid1 = sheet1.cell_value(index1,hid_index1)
				if hid1 == sheet2.cell_value(index2,hid_index2):
					# 如果两个表的id相同则把表1的单元内容设置到表2对应的单元格
					write_sheet.write(index2,col_index,sheet1.cell_value(index1,target_index1))
 
# 保存新的文件
write_workbook.save('new.xls')

结果

img

注意:

此脚本里边的表格式是 xls,不能使用xlsx格式

上边是只匹配了1列,我改了一下脚本,可以匹配多行

import shutil
import sys
import xlwt
import xlrd
 
file1 = "C:\\Users\\admin\\Desktop\\表11.xls"
#打开表1
wb1 = xlrd.open_workbook(filename=file1)
# 表1要匹配的列索引
hid_index1 = 0
# 表1目标数据列索引
target_index1 = 1
target_index2 = 2
target_index3 = 3
target_index4 = 4
# 表1的sheet
sheet1 = wb1.sheet_by_index(0)
# 表1的sheet的总行数
rowNum1 = sheet1.nrows
# 表1的sheet的总列数
colNum1 = sheet1.ncols
 
file2 = "C:\\Users\\admin\\Desktop\\表22.xls"
#打开表2
wb2 = xlrd.open_workbook(filename=file2)#打开文件
# 表2要匹配的列索引
hid_index2 = 0
# 表2目标数据列索引
target_indexb = 1
target_indexc = 2
target_indexd = 3
target_indexe = 4
# 表2的sheet
sheet2 = wb2.sheet_by_index(0)#通过索引获取表格
# 表2的sheet的总行数
rowNum2 = sheet2.nrows
# 表2的sheet的总列数
colNum2 = sheet2.ncols
 
# xlwt准备生成一个新的文件
write_workbook = xlwt.Workbook()
write_sheet = write_workbook.add_sheet('sheet1',cell_overwrite_ok=True)
 
for index2 in range(0,rowNum2):
	for col_index in range(0,colNum2):
		# 遍历表2的每一行每一列,把对应的单元设置到新的文件中,即复制了表2的数据
		write_sheet.write(index2,col_index,sheet2.cell_value(index2,col_index))
		# 在遍历列过程中,如果碰到目标数据列索引.即需要补充的字段,则进行遍历表1,判断的id索引匹配
		if col_index == target_indexb:
			for index1 in range(1,rowNum1):
				hid1 = sheet1.cell_value(index1,hid_index1)
				if hid1 == sheet2.cell_value(index2,hid_index2):
					# 如果两个表的id相同则把表1的单元内容设置到表2对应的单元格
					write_sheet.write(index2,col_index,sheet1.cell_value(index1,target_index1))
		if col_index == target_indexc:
			for index1 in range(1,rowNum1):
				hid1 = sheet1.cell_value(index1,hid_index1)
				if hid1 == sheet2.cell_value(index2,hid_index2):
					# 如果两个表的id相同则把表1的单元内容设置到表2对应的单元格
					write_sheet.write(index2,col_index,sheet1.cell_value(index1,target_index2))
		if col_index == target_indexd:
			for index1 in range(1,rowNum1):
				hid1 = sheet1.cell_value(index1,hid_index1)
				if hid1 == sheet2.cell_value(index2,hid_index2):
					# 如果两个表的id相同则把表1的单元内容设置到表2对应的单元格					
					write_sheet.write(index2,col_index,sheet1.cell_value(index1,target_index3))
		if col_index == target_indexe:
			for index1 in range(1,rowNum1):
				hid1 = sheet1.cell_value(index1,hid_index1)
				if hid1 == sheet2.cell_value(index2,hid_index2):
					# 如果两个表的id相同则把表1的单元内容设置到表2对应的单元格					
					write_sheet.write(index2,col_index,sheet1.cell_value(index1,target_index4))
 
# 保存新的文件
write_workbook.save('new.xls')

标签:index,匹配,target,Python,excel,write,index2,sheet,index1
From: https://www.cnblogs.com/jianchen013/p/16949374.html

相关文章

  • Python sql转字典
    问题返回给前端需提供键值对格式,原本查询数据库只返回值。 解决创建游标对象时填入参数cursor=pymysql.cursors.DictCursorsql="SELECTuser_id,email,nati......
  • 3、使用 TVMC Python 入门:TVM 的高级 API
    本节将介绍针对TVM初学者设计的脚本工具。开始前如果没有下载示例模型,需要先通过终端下载resnet模型:cdmyscripts/mvresnet50-v2-7-frozen.onnxmy_model.onnx......
  • PYTHON 判断引用路径的类型
    PYTHON判断引用路径的类型如下方法,用于实现在不加载模块的前提下,判断某一个python引用路径是否存在,以及属于什么类型(模块目录,模块文件,类名).用于在一些不便......
  • AI人工智能-python
    AI概念语音合成可将文字信息转化为声音信息语音识别可将语音识别为文字百度ai平台页面熟悉              创建应用     ......
  • Excel二维码图片生成器
    Excel二维码图片生成器它可以将excel文件的数据,每行数据生成一张二维码图片,并保存到电脑。软件无需安装,解压后即可直接使用,无需联网,操作简便快捷。 步骤1:导入事先制作......
  • 【python】使用百度api进行音频文件转写
     【python】使用百度api进行音频文件转写脚本目标:智能云的音频文件转写文档只给了个demo,每次只能传1分钟以内的音频啥的,不好直接用,简单打包一下,做到把音频放文件夹,直......
  • python循环
    python学习1.for循环range()函数foriinrange(1,10):print(i)#输出为123456789该函数还可以制定步长如:foriinrange(1,10,2)即为以2为步长,在1到......
  • python报错 ModuleNotFoundError: No module named ‘win32api‘
    参考链接https://blog.csdn.net/weixin_43149311/article/details/120806116报错信息如下:ModuleNotFoundError:Nomodulenamed‘win32api‘解决方法参考1.重新......
  • 项目实战中使用枚举类enum的 values() 方法进行枚举元素匹配优化
    验证代码枚举类型publicenumOrderStatusEnum{CREATE(1),COMPLETE(10);privateintstatus;OrderStatusEnum(intstatus){this.status=status......
  • 在linux虚拟机中运行python
    在linux虚拟机中运行python方法1:运用python指令运行一般情况linux系统会自动安装python所以在终端中输入python3就自动进入python的交互模式输入ctrl+z退出交互模式......