python python的pickle文件是跨平台的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1849523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is pickle file of python cross-platform?
提问by vikithakar
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???
我已经创建了我的一个小的 python 脚本。我在 Linux 上保存了泡菜文件,然后在 Windows 上使用它,然后再次在 Linux 上使用它,但现在该文件在 Linux 上不起作用,但它在 Windows 上运行良好。是这样,python 是 coss-platform 但pickle 文件不是。这个有什么解决办法吗???
回答by taleinat
Python's pickle is perfectly cross-platform.
Python 的pickle 是完全跨平台的。
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.
这可能是由于 Windows 和 Linux 之间的 EOL(行尾)差异所致。确保在写入和读取它们时以二进制模式打开泡菜文件,分别使用 open() 的“wb”和“rb”模式。
Note: Passing pickles between different versions of Python can cause trouble, so try to have the same version on both platforms.
注意:在不同版本的 Python 之间传递 pickle 会引起麻烦,所以尽量在两个平台上使用相同的版本。
回答by Greg Hewgill
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.
该pickle
模块支持几种不同的数据格式。如果您指定特定的 pickle 格式而不是使用默认值 (0),您可能会遇到跨平台二进制文件问题。您可以通过指定协议 0 来使用纯 ASCII pickle 文件。
回答by ironchefpython
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.
Pickle 应该是跨平台的,存在版本控制/协议问题,(参见http://docs.python.org/library/pickle.html#data-stream-format)但一般来说,如果您使用相同版本的python 在你的 windows 和 unix 机器上,它们应该是可互操作的。
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.
如果您使用 pickle 作为数据传输机制,您可能需要考虑用于数据存储的实现较少的特定格式,例如 json、xml、csv、yaml 等。
回答by catchmeifyoutry
Maybe you don't open the file in binary mode? See this stackoverflow question
也许您没有以二进制模式打开文件?看到这个stackoverflow问题
回答by akaihola
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.
一个值得尝试的有趣想法是PyON(Python 对象表示法)。根据我的测试,当前版本似乎至少适用于简单的情况。不过,对于该项目是否是一个好主意,邮件列表似乎存在一些分歧。
回答by steveha
You could use json
instead of pickle
. If it can save your data, you know it's cross platform.
您可以使用json
代替pickle
. 如果它可以保存您的数据,您就知道它是跨平台的。