Is pickle file of python cross-platform?

2019-01-14 22:25发布

I have created a small python script of mine. I saved the pickle file on Linux and then used it on windows and then again used it back on Linux but now that file is not working on Linux but it is working perfectly on windows. Is is so that python is coss-platform but the pickle file is not. Is there any solution to this one???

6条回答
闹够了就滚
2楼-- · 2019-01-14 23:02

One interesting idea to try out is PyON (Python Object Notation). The current version seems to work at least for simple cases according to my tests. There seems to have been some disagreement on mailing lists whether the project is a good idea, though.

查看更多
叛逆
3楼-- · 2019-01-14 23:04

Pickle should be cross-platform, there are versioning/protocol issues, (see http://docs.python.org/library/pickle.html#data-stream-format) but in general if you're using the same release of python on your windows and unix boxes, they should be interoperable.

If you're using pickle as a data transport mechanism, you might want to consider less-implementation specific formats for data storage, such as json, xml, csv, yaml, etc.

查看更多
神经病院院长
4楼-- · 2019-01-14 23:08

Python's pickle is perfectly cross-platform.

This is likely due to EOL (End-Of-Line) differences between Windows and Linux. Make sure to open your pickle files in binary mode both when writing them and when reading them, using open()'s "wb" and "rb" modes respectively.

Note: Passing pickles between different versions of Python can cause trouble, so try to have the same version on both platforms.

查看更多
欢心
5楼-- · 2019-01-14 23:08

Maybe you don't open the file in binary mode? See this stackoverflow question

查看更多
小情绪 Triste *
6楼-- · 2019-01-14 23:09

You could use json instead of pickle. If it can save your data, you know it's cross platform.

查看更多
做个烂人
7楼-- · 2019-01-14 23:11

The pickle module supports several different data formats. If you are specifying a particular pickle format instead of using the default (0), you may be running into cross-platform binary file problems. You can use plain ASCII pickle files by specifying protocol 0.

查看更多
登录 后发表回答