Load data from Python pickle file in a loop?

前端 未结 1 985
自闭症患者
自闭症患者 2021-02-09 16:25

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 16:49

    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)
提交回复
热议问题