Load data from Python pickle file in a loop?

前端 未结 1 1206
滥情空心
滥情空心 2021-02-09 16:33

In a small data-acquisition project we use the Python\'s pickle to store recorded data, i.e. for each \"event\" we add it to the output file f with

1条回答
  •  野的像风
    2021-02-09 17:03

    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()
    

    0 讨论(0)
提交回复
热议问题