我有好几千年的excel文件。所有文件都在其名称中嵌入了年份(例如2010年,2011年等)。某些excel文件包含工作表中的(Closure,closure)等关键字。这些关键字可以存在于句子中(例如,由于构造而封闭道路)。我想编写一个代码,以便我可以复制特定年份的文件,这些文件在其工作表中没有这些关键字。以下代码正在运行,但速度太慢。任何想法如何让它更快?
import glob
import os
import shutil
import xlrd
os.chdir("G:/_Traffic Counts/Traffic Volumes/")
choice = raw_input("Specify the Year to Copy (e.g. 2012) and Press Enter ")
location = ("C:/Users/tsengineer/Desktop/Trial_JM/")
notcopy = ("closure", "Closure")
i = 0
for file in glob.glob("*.xls"):
if choice in file:
book = xlrd.open_workbook(file)
sheet = book.sheet_by_index(0)
for row in range(sheet.nrows):
for column in range(sheet.ncols):
if any(s in str(sheet.cell(row,column).value) for s in notcopy):
pass
else:
shutil.copy(file, location)
print "Copying...", i+1
i = i + 1
print "Copying Complete. Total File Copied:", i