在包含字符串的文件中搜索行的最快方法是什么.我有一个包含要搜索的字符串的文件.这个小文件(smallF)包含大约50,000行,看起来像:
stringToSearch1
stringToSearch2
stringToSearch3
我必须在一个更大的文件中搜索所有这些字符串(大约1亿行).如果此较大文件中的任何行包含搜索字符串,则会打印该行.
到目前为止,我提出的最佳方法是
grep -F -f smallF largeF
Run Code Online (Sandbox Code Playgroud)
但这不是很快.在smallF中只有100个搜索字符串,大约需要4分钟.对于超过50,000个搜索字符串,将花费大量时间.
有更有效的方法吗?
我正在尝试创建一个简单的应用程序,其中
将图像推送到目录中(通过外部进程)
Python看门狗触发器,图像由函数处理,结果显示在窗口中
作业连续运行,并且在图像进入目录时触发处理功能。结果的绘图窗口应仅用新结果更新,而不是关闭窗口然后重新绘图。
下面的代码不显示结果。绘图窗口保持空白,然后崩溃。如果除matplotlib之外的其他工具可以轻松完成此工作,那也很好。
# plt is matplotlib.pyplot
def process_and_plot(test_file):
y, x = getresults(test_file) # function which returns results on image file
y_pos = range(len(y))
plt.figure(num=1,figsize=(20,10))
plt.bar(y_pos, y, align='center')
plt.xticks(y_pos, x)
plt.show()
# to trigger the proess_and_plt function when a new file comes in directory
class ExampleHandler(FileSystemEventHandler):
def on_created(self, event):
print event.src_path
process_and_plot(event.src_path)
event_handler = ExampleHandler()
observer.schedule(event_handler, path='path/to/directory')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Run Code Online (Sandbox Code Playgroud)