[英]Load data from Python pickle file in a loop?
In a small data-acquisition project we use the Python's pickle
to store recorded data, ie for each "event" we add it to the output file f
with 在一个小型数据采集项目中,我们使用Python的
pickle
来存储记录的数据,即对于每个“事件”,我们将它添加到输出文件f
中
pkl.dump(event, f, pkl.HIGHEST_PROTOCOL)
where import cPickle as pkl
. 其中
import cPickle as pkl
。
In the analysis of the data we read each event, but in contrast to a normal file where processing can be one in a rather elegant way: 在分析数据时,我们读取每个事件,但与普通文件相比,处理可以是一个相当优雅的方式:
with open(filename) as f:
for line in f:
do_something()
looping over all the data in a pickle file this becomes a bit more awkward: 循环遍历pickle文件中的所有数据,这变得有点尴尬:
with open(filename) as f:
try:
while True:
event = pkl.load(f)
do_something()
except (EOFError, UnpicklingError):
pass
Is it possible to make pickle reading more like the example for regular files above? 是否有可能使pickle阅读更像上面常规文件的示例?
Yes, indeed. 确实是的。 Use this generator below to make the events readable in a loop:
使用下面的这个生成器可以在循环中读取事件:
def pickleLoader(pklFile):
try:
while True:
yield pkl.load(pklFile)
except EOFError:
pass
Now you can simply write: 现在你可以简单地写:
with open(filename) as f:
for event in pickleLoader(f):
do_something()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.