[英]Can you pre-compile regular expressions in Python, save them to a file, and then reload them from the file?
I have an application that loads a large table of regular expressions from an Excel file, compiles them, and then uses them to perform its function.我有一个应用程序,它从 Excel 文件加载一个大的正则表达式表,编译它们,然后使用它们来执行它的 function。 It takes about 2 minutes for Python to compile the regular expressions, which will increase as I add more expressions to the Excel file.
Python 编译正则表达式大约需要 2 分钟,随着我向 Excel 文件中添加更多表达式,这将增加。 The Excel file does not change often, so I would like to avoid the two-minute+ startup time whenever the Excel file has not changed.
Excel 文件不经常更改,所以我想避免每当 Excel 文件没有更改时的两分钟以上的启动时间。
Is there a way to cache the compiled regular expressions to a file that I can load when the Excel file hasn't changed?当 Excel 文件未更改时,有没有办法将编译的正则表达式缓存到我可以加载的文件中?
You can store the compiled regular expressions in pickle files:您可以将编译的正则表达式存储在 pickle 文件中:
import re
import pickle
r1 = re.compile('\d\d\d')
with open('tmp', 'wb') as fh:
pickle.dump(r1, fh)
with open('tmp', 'rb') as fh:
r2 = pickle.load(fh)
print(r2.match('673'))
<re.Match object;
<re.Match object; span=(0, 3), match='673'>
跨度=(0, 3),匹配='673'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.