如果文件已经使用 Python 加载到内存中,如何跳过加载文件?
How to skip loading file if it is already loaded in memory using Python?
我有以下代码,我试图避免加载 symspell.pkl
文件,一旦它已经加载到内存中:
from symspellpy import SymSpell
if 'sym_spell' in globals():
print('sym_spell is already loaded!')
sym_spell = global()['sym_spell']
esle:
print('loading sym_spell...')
sym_spell = SymSpell(max_dictionary_edit_distance=5, prefix_length=7)
sym_spell.load_pickle('symspell.pkl')
但似乎 python 总是执行 else
语句,即使 if
语句是 True
.
以下是我的问题:
谁能解释为什么 Python 总是在这里执行 else
语句?
如果文件已经加载到 Python 中的内存中,我尝试跳过加载文件的正确方法是什么?如果不行,有没有更好的办法?
我正在使用 python3.8.2.
提前致谢。
我不明白您要做什么,但是 if
语句始终为 False,因为全局变量中没有“sym_spell”。我认为您正在尝试检查“SymSpell”而不是“sym_spell”。
编辑:
Is what I tried a right way to skip loading file if it is already loaded in memory in Python? If not, is there a better way?
不,我不认为是否有办法完全满足要求。但是,如果您的文件非常大,并且您不想一次又一次地加载相同的数据。那么,这是最适合您的解决方案。 Yoi可以看看Jupyter Lab or Jupyter Notebook.
Jupyter 实验室:
JupyterLab: Jupyter’s Next-Generation Notebook Interface
JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data. JupyterLab is flexible: configure and arrange the user interface to support a wide range of workflows in data science, scientific computing, and machine learning. JupyterLab is extensible and modular: write plugins that add new components and integrate with existing ones.
Jupyter 笔记本:
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
在这里您可以一次加载数据,无需导入该数据就可以多次使用。您现在可以直接从您的浏览器对其进行测试,看看它是如何运行的。从那里 official website。我认为这对你很有用。
我正在发布我的修复程序,以防它可能有所帮助。
经过多次尝试,我使用 builtins
模块(而不是 globals()
)成功了
和 try/except
语句如下:
import builtins
from symspellpy import SymSpell
try:
if builtins.sym_spell:
print('sym_spell is already loaded!')
sym_spell = builtins.sym_spell
except:
print('sym_spell is not yet loaded! loading sym_spell...')
sym_spell = SymSpell(max_dictionary_edit_distance=5, prefix_length=7)
sym_spell.load_pickle('symspell.pkl')
builtins.sym_spell = sym_spell
我有以下代码,我试图避免加载 symspell.pkl
文件,一旦它已经加载到内存中:
from symspellpy import SymSpell
if 'sym_spell' in globals():
print('sym_spell is already loaded!')
sym_spell = global()['sym_spell']
esle:
print('loading sym_spell...')
sym_spell = SymSpell(max_dictionary_edit_distance=5, prefix_length=7)
sym_spell.load_pickle('symspell.pkl')
但似乎 python 总是执行 else
语句,即使 if
语句是 True
.
以下是我的问题:
谁能解释为什么 Python 总是在这里执行
else
语句?如果文件已经加载到 Python 中的内存中,我尝试跳过加载文件的正确方法是什么?如果不行,有没有更好的办法?
我正在使用 python3.8.2.
提前致谢。
我不明白您要做什么,但是 if
语句始终为 False,因为全局变量中没有“sym_spell”。我认为您正在尝试检查“SymSpell”而不是“sym_spell”。
编辑:
Is what I tried a right way to skip loading file if it is already loaded in memory in Python? If not, is there a better way?
不,我不认为是否有办法完全满足要求。但是,如果您的文件非常大,并且您不想一次又一次地加载相同的数据。那么,这是最适合您的解决方案。 Yoi可以看看Jupyter Lab or Jupyter Notebook.
Jupyter 实验室:
JupyterLab: Jupyter’s Next-Generation Notebook Interface JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data. JupyterLab is flexible: configure and arrange the user interface to support a wide range of workflows in data science, scientific computing, and machine learning. JupyterLab is extensible and modular: write plugins that add new components and integrate with existing ones.
Jupyter 笔记本:
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
在这里您可以一次加载数据,无需导入该数据就可以多次使用。您现在可以直接从您的浏览器对其进行测试,看看它是如何运行的。从那里 official website。我认为这对你很有用。
我正在发布我的修复程序,以防它可能有所帮助。
经过多次尝试,我使用 builtins
模块(而不是 globals()
)成功了
和 try/except
语句如下:
import builtins
from symspellpy import SymSpell
try:
if builtins.sym_spell:
print('sym_spell is already loaded!')
sym_spell = builtins.sym_spell
except:
print('sym_spell is not yet loaded! loading sym_spell...')
sym_spell = SymSpell(max_dictionary_edit_distance=5, prefix_length=7)
sym_spell.load_pickle('symspell.pkl')
builtins.sym_spell = sym_spell