ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?

前端 未结 2 1348
陌清茗
陌清茗 2020-12-07 16:05

I use pickle to dump a file on python 3, and I use pickle to load the file on python 2, the ValueError appears.

So, python 2 pickle can not load the file dumped by

2条回答
  •  忘掉有多难
    2020-12-07 16:13

    You should write the pickled data with a lower protocol number in Python 3. Python 3 introduced a new protocol with the number 3 (and uses it as default), so switch back to a value of 2 which can be read by Python 2.

    Check the protocolparameter in pickle.dump. Your resulting code will look like this.

    pickle.dump(your_object, your_file, protocol=2)
    

    There is no protocolparameter in pickle.load because pickle can determine the protocol from the file.

提交回复
热议问题