【发布时间】:2012-10-24 19:01:36
【问题描述】:
我有一个脚本,它使用 xlrd 比较两个 .xls 工作表之间的值。默认情况下,它可以让我查找相似之处,并将它们写入 txt 文件。如果我传入一个参数,我可以查找每个 .xls 独有的设备,并将其写入 txt 文件。现在,我遍历每个 .xls 中的行,并在发现两个文件之间存在相似性时将标志标记为 True。当标志标记为 False 时,它只会从第一张表写入主机。这是带有标志的代码:
else:
for row1 in range(sheet1.nrows):
inboth = False
for row2 in range(sheet2.nrows):
if sheet2.row_values(row2)[0].split(".")[0] == sheet1.row_values(row1)[0].split(".")[0]:
inboth = True
if not inboth:
outfile.write(sheet1.row_values(row1)[0].split(".")[0] + ",File1\n")
for row2 in range(sheet2.nrows):
inboth = False
for row1 in range(sheet1.nrows):
if sheet1.row_values(row1)[0].split(".")[0] == sheet2.row_values(row2)[0].split(".")[0]:
inboth = True
if not inboth:
outfile.write(sheet2.row_values(row2)[0].split(".")[0] + ",File2\n")
有没有更有效的方法可以在不使用“inboth”标志的情况下做到这一点?有没有一种解决方案,我不需要对两张表进行两次迭代?
【问题讨论】:
-
测试总数为 2xNxM,其中 N 和 M 分别是 scheet1 和 scheet2 中的行数。这是计算机科学语言中的 O(N^2),并且会让您在处理大型数据集时编码极其缓慢。
标签: python excel loops flags xlrd