下面是我目前所拥有的——这是我在其他论坛上看到的东西的组合。
所有Excel电子表格的格式都相同,我要查找的字符串始终位于B列。
理想情况下,如果我能搜索一个字符串并让它返回文件名,我会很乐意的。
import os
keyword = input("Search For?: ") # ask the user for keyword, use raw_input() on Python 2.x
root_dir = "DIRECTORY" # path to the root directory to search
for root, dirs, files in os.walk(root_dir, onerror=None): # walk the root dir
for filename in files: # iterate over the files in the current dir
file_path = os.path.join(root, filename) # build the file path
try:
with open(file_path, "rb") as f: # open the file for reading
# read the file line by line
for line in f: # use: for i, line in enumerate(f) if you need line numbers
try:
line = line.decode("utf-8") # try to decode the contents to utf-8
except ValueError: # decoding failed, skip the line
continue
if keyword in line: # if the keyword exists on the current line...
print(file_path) # print the file path
break # no need to iterate over the rest of the file
except (IOError, OSError): # ignore read and permission errors
pass