python的pickle文件是跨平台的吗?

发布于 2024-08-14 04:30:21 字数 174 浏览 3 评论 0 原文

我创建了一个我的小 python 脚本。我在 Linux 上保存了 pickle 文件,然后在 Windows 上使用它,然后再次在 Linux 上使用它,但现在该文件在 Linux 上不起作用,但在 Windows 上运行得很好。 是这样,python 是 coss 平台,但 pickle 文件不是。 这个有什么解决办法吗???

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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

握住你手 2024-08-21 04:30:21

Python 的 pickle 是完美的跨平台的。

这可能是由于 Windows 和 Linux 之间的 EOL(行尾)差异造成的。确保在写入和读取时以二进制模式打开 pickle 文件,分别使用 open() 的“wb”和“rb”模式。

注意:在不同版本的 Python 之间传递 pickle 可能会导致麻烦,因此请尝试在两个平台上使用相同的版本。

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.

小忆控 2024-08-21 04:30:21

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.

甜是你 2024-08-21 04:30:21

也许您没有以二进制模式打开文件?请参阅此 stackoverflow 问题

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

莫多说 2024-08-21 04:30:21

Pickle 应该是跨平台的,存在版本控制/协议问题,(参见 http://docs.python.org/library/pickle.html#data-stream-format),但一般来说,如果您在 Windows 和 Unix 机器上使用相同版本的 Python,它们应该是可互操作的。

如果您使用 pickle 作为数据传输机制,您可能需要考虑较少实现的特定数据存储格式,例如 json、xml、csv、yaml 等。

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.

半世晨晓 2024-08-21 04:30:21

您可以使用 json 而不是 pickle。如果它可以保存您的数据,您就知道它是跨平台的。

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

So尛奶瓶 2024-08-21 04:30:21

一个值得尝试的有趣想法是 PyON(Python 对象表示法)。根据我的测试,当前版本似乎至少适用于简单的情况。不过,对于该项目是否是一个好主意,邮件列表上似乎存在一些分歧。

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.

红尘作伴 2024-08-21 04:30:21

当 Pickle 腌制一个对象时,它会存储用于创建该对象的模块和方法以及提供给该可调用对象的参数。

>>> import pathlib
>>> import pickle
>>> import pickletools
>>> myPath = pickle.dumps(pathlib.Path.home())
>>> pickletools.dis(pickletools.optimize(myPath))

    0: \x80 PROTO      3
    2: c    GLOBAL     'pathlib WindowsPath'
   23: X    BINUNICODE 'C:\\'
   31: X    BINUNICODE 'Users'
   41: X    BINUNICODE '<your_username>'
   59: \x87 TUPLE3
   60: R    REDUCE
   61: .    STOP
highest protocol among opcodes = 2

从概念上讲,unpickle 确实如此:

from pathlib import WindowsPath
return WindowsPath('C:\\', 'Users', '<your_username>')

Windows 和 Linux 之间的几个类和方法在底层是不同的,或者两个系统之间有不同的行为。

  • pathlib.Path() 将根据环境创建 WindowsPath 或 PosixPath。
  • os.path 在 Windows 上实际上是 ntpath,在 Linux 上是 posixpath。

同样,如果您在系统之间有不同的模块(或不同版本的模块)可访问,或者对象的构造函数发生变化,这将使 pickle 不兼容。

同样,通常您必须从同一个 ma​​in 加载和转储 pickle 文件。如果您运行两个单独的脚本

serialize_a.py:

class A:
    def __init__(self, num):
        self.num = num

if __name__ == "__main__":
    import pickle
    with open('pickle.pkl', 'wb') as f:
        pickle.dump(A(42), f)
import pickle
import serialize_a

if __name__ == "__main__":
    with open('pickle.pkl', 'rb') as f:
        pickle.load(f)

您将收到错误,因为pickle将A存储为__main__,但现在代码需要将其查找为< code>serialize_a.A():

AttributeError: 无法从 ...> 获取

When Pickle pickles an object, it stores the module and method used to create it and the arguments to feed to that callable.

>>> import pathlib
>>> import pickle
>>> import pickletools
>>> myPath = pickle.dumps(pathlib.Path.home())
>>> pickletools.dis(pickletools.optimize(myPath))

    0: \x80 PROTO      3
    2: c    GLOBAL     'pathlib WindowsPath'
   23: X    BINUNICODE 'C:\\'
   31: X    BINUNICODE 'Users'
   41: X    BINUNICODE '<your_username>'
   59: \x87 TUPLE3
   60: R    REDUCE
   61: .    STOP
highest protocol among opcodes = 2

Conceptually, unpickling this just does:

from pathlib import WindowsPath
return WindowsPath('C:\\', 'Users', '<your_username>')

Several classes and methods are, under the hood, different between Windows and Linux, or have different behaviour between the two systems.

  • pathlib.Path() will create WindowsPath or PosixPath depending on environment.
  • os.path will actually be ntpath on Windows and posixpath on Linux.

Likewise, if you have different modules (or different versions of modules) accessible between systems, or the constructor of your object changes, that will make the pickle incompatible.

Likewise, in general you must load and dump pickle files from the same main. If you run two separate scripts

serialize_a.py:

class A:
    def __init__(self, num):
        self.num = num

if __name__ == "__main__":
    import pickle
    with open('pickle.pkl', 'wb') as f:
        pickle.dump(A(42), f)
import pickle
import serialize_a

if __name__ == "__main__":
    with open('pickle.pkl', 'rb') as f:
        pickle.load(f)

You'll get an error because the pickle stores A as being in __main__, but now the code needs to find it as serialize_a.A():

AttributeError: Can't get attribute 'A' on <module '__main__' from ...>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文