Python XLRD:已成功从2张工作表中提取了2个列表,但列表比较无法工作
好的,我有两张xlsx表,两张表的第二列,索引1,都有一个sim卡号码列表。在使用xlrd提取数据后,我已成功地将这两列的内容作为两个列表打印到我的powershell终端中,以及这些列表中的元素数量 第一张工作表有454个条目,第二张工作表有361个条目。我需要找到第二张纸上不存在的93,然后把它们放入无援助的模拟人生中。当然,我可以手动完成这项任务,但我希望在将来不可避免地需要再次执行此任务时,能够自动完成此任务,因此我正在尝试编写此python脚本 考虑到python同意我有454个条目的列表和361个条目的列表,我想我只需要找出一个列表比较,我研究了堆栈溢出,用3种不同的解决方案尝试了3次,但每次,当我使用该脚本生成第三个列表时,上面写着454…这意味着它没有删除小列表中重复的条目。请告知Python XLRD:已成功从2张工作表中提取了2个列表,但列表比较无法工作,python,excel,xlrd,Python,Excel,Xlrd,好的,我有两张xlsx表,两张表的第二列,索引1,都有一个sim卡号码列表。在使用xlrd提取数据后,我已成功地将这两列的内容作为两个列表打印到我的powershell终端中,以及这些列表中的元素数量 第一张工作表有454个条目,第二张工作表有361个条目。我需要找到第二张纸上不存在的93,然后把它们放入无援助的模拟人生中。当然,我可以手动完成这项任务,但我希望在将来不可避免地需要再次执行此任务时,能够自动完成此任务,因此我正在尝试编写此python脚本 考虑到python同意我有454个条目的
from os.path import join, dirname, abspath
import xlrd
theirBookFileName = join(dirname(dirname(abspath(__file__))), 'pycel', 'theirBook.xlsx')
ourBookFileName = join(dirname(dirname(abspath(__file__))), 'pycel', 'ourBook.xlsx')
theirBook = xlrd.open_workbook(theirBookFileName)
ourBook = xlrd.open_workbook(ourBookFileName)
theirSheet = theirBook.sheet_by_index(0)
ourSheet = ourBook.sheet_by_index(0)
theirSimColumn = theirSheet.col(1)
ourSimColumn = ourSheet.col(1)
numColsTheirSheet = theirSheet.ncols
numRowsTheirSheet = theirSheet.nrows
numColsOurSheet = ourSheet.ncols
numRowsOurSheet = ourSheet.nrows
# First Attempt at the comparison, but fails and returns 454 entries from the bigger list
unpaidSims = [d for d in theirSimColumn if d not in ourSimColumn]
print unpaidSims
lengthOfUnpaidSims = len(unpaidSims)
print lengthOfUnpaidSims
print "\nWe are expecting 93 entries in this new list"
# Second Attempt at the comparison, but fails and returns 454 entries from the bigger list
s = set(ourSimColumn)
unpaidSims = [x for x in theirSimColumn if x not in s]
print unpaidSims
lengthOfUnpaidSims = len(unpaidSims)
print lengthOfUnpaidSims
# Third Attempt at the comparison, but fails and returns 454 entries from the bigger list
unpaidSims = tuple(set(theirSimColumn) - set(ourSimColumn))
print unpaidSims
lengthOfUnpaidSims = len(unpaidSims)
print lengthOfUnpaidSims
根据,col方法返回给定列中的单元格对象序列
它没有提到任何关于单元格对象比较的内容。深入研究,他们似乎没有在类中编写任何比较方法。因此,状态表示将按对象标识对对象进行比较。换句话说,除非它们是Cell类的完全相同的实例,否则比较将是错误的,即使它们包含的值相同
您需要比较单元格的值。例如:
unpaidSims = set(sim.value for sim in theirSimColumn) - set(sim.value for sim in ourSimColumn)
您确定较小列表中的所有条目也在较大列表中吗?