在Mac / Linux上不会加载腌制文件

时间:2023-02-06 12:30:34

I have an application that imports data from a pickled file. It works just fine in Windows but Mac and Linux behaviour is odd.

我有一个从pickle文件导入数据的应用程序。它适用于Windows,但Mac和Linux的行为很奇怪。

In OS X, the pickled file (file extension ".char") is unavailable as a selection unless I set the file type to *.*. Then, if I select a file that has the .char extension, it won't load, giving the error

在OS X中,除非我将文件类型设置为*。*,否则pickled文件(文件扩展名“.char”)不可用作选择。然后,如果我选择一个具有.char扩展名的文件,它将不会加载,从而产生错误

unpickle_file = cPickle.load(char_file) 

ValueError: could not convert string to float

ValueError:无法将字符串转换为float

However, if I create a file that doesn't have the .char extension, that file will load up just fine.

但是,如果我创建一个没有.char扩展名的文件,该文件将加载正常。

In Linux, when I use the "file open" dialog, my pickled files aren't visible, whether or not they have a file extension. However, I can see them under Nautilus or Dolphin. They simply don't exist to my application though.

在Linux中,当我使用“文件打开”对话框时,我的pickle文件是不可见的,无论它们是否具有文件扩展名。但是,我可以在Nautilus或Dolphin下看到它们。它们根本不适用于我的应用程序。


Edit Here's the save code:

编辑这是保存代码:

def createSaveFile(self):
        """Create the data files to be saved and save them.

        Creates a tuple comprised of a dictionary of general character information
        and the character's skills dictionary."""
        if self.file_name:
            self.save_data = ({'Name':self.charAttribs.name,

              <snip> 

                self.charAttribs.char_skills_dict)
            self.file = open(self.file_name, 'w')
            cPickle.dump(self.save_data, self.file)
        self.file.close()

Here's the open code:

这是开放代码:

 def getCharFile(self, event): # wxGlade: CharSheet.<event_handler>
        """Retrieve pickled character file from disk."""
        wildcard = "Character files (*.char) | *.char | All files (*.*) | *.*"        
        openDialog = wx.FileDialog(None, "Choose a character file", os.getcwd(),
        "", wildcard, wx.OPEN | wx.CHANGE_DIR)
        if openDialog.ShowModal() == wx.ID_OK:
            self.path = openDialog.GetPath()
        try:
            char_file =  open(self.path, "r")
            unpickle_file = cPickle.load(char_file)
            char_data, char_skills = unpickle_file
            self.displayCharacter(char_data, char_skills)
        except IOError:
            self.importError = wx.MessageDialog(self, 
            "The character file is not available!",
            "Character Import Error", wx.OK | wx.ICON_ERROR)
            self.importError.ShowModal()
            self.importError.Destroy()
            openDialog.Destroy()

5 个解决方案

#1


10  

Probably you didn't open the file in binary mode when writing and/or reading the pickled data. In this case newline format conversion will occur, which can break the binary data.

在写入和/或读取pickle数据时,您可能没有以二进制模式打开文件。在这种情况下,将发生换行格式转换,这可能会破坏二进制数据。

To open a file in binary mode you have to provide "b" as part of the mode string:

要以二进制模式打开文件,您必须提供“b”作为模式字符串的一部分:

char_file = open('pickle.char', 'rb')

#2


8  

As mentioned by Adam, the problem is likely to be the newline format of the pickle file.

正如Adam所提到的,问题很可能是pickle文件的换行格式。

Unfortunately, the real problem is actually caused on save rather than load. This may be recoverable if you're using text mode pickles, rather than binary. Try opening the file in universal newline mode, which will cause python to guess what the right line-endings are ie:

不幸的是,真正的问题实际上是由保存而不是负载引起的。如果您使用的是文本模式pickles而不是binary,那么这可能是可恢复的。尝试以通用换行模式打开文件,这将导致python猜测正确的行结尾是什么,即:

char_file=open('filename.char','rU')

However, if you're using a binary format (cPickle.dump(file, 1)) you may have an unrecoverably corrupted pickle (even when loading in Windows) - if you're lucky and no \r\n characters show up then it may work, but as soon as this occurs you could end up with corrupted data, as there's no way to distinguish between a "real" \r\n code and one windows has inserted on seeing just \n.

但是,如果您使用的是二进制格式(cPickle.dump(file,1)),您可能会有一个不可恢复的损坏的泡菜(即使在Windows中加载) - 如果您很幸运并且没有\ r \ n字符出现,那么它可能会起作用,但是一旦发生这种情况你就可能最终得到损坏的数据,因为没有办法区分“真正的”\ r \ n代码和一个窗口已经插入只看到\ n。

The best way to handle things to be loaded in multiple platforms is to always save in binary mode. On your windows machine, when saving the pickle use:

处理要在多个平台上加载的东西的最佳方法是始终以二进制模式保存。在您的Windows机器上,保存泡菜时使用:

char_file = open('filename.char','wb')
cPickle.dumps(data, char_file)

#3


4  

Another way to get this error is to forget to close the output file after pickling. This can leave an incomplete file that fails in random ways during subsequent unpickling.

获得此错误的另一种方法是忘记在酸洗后关闭输出文件。这可能会留下一个不完整的文件,在随后的unpickling中会以随机方式失败。

#4


2  

self.file = open(self.file_name, 'w')

Should be:

self.file = open(self.file_name, 'wb')

In your createSaveFile function, to save the file in binary mode (rather than text mode). You should also make sure you open the file in binary mode as well (rb).

在createSaveFile函数中,以二进制模式(而不是文本模式)保存文件。您还应该确保以二进制模式打开文件(rb)。

If you don't use binary mode then Windows will convert all new-lines to \r\n and will effectively corrupt the file (at least as far as other OS's are concerned).

如果你不使用二进制模式,那么Windows会将所有新行转换为\ r \ n并且会有效地破坏文件(至少就其他操作系统而言)。

#5


0  

Use dos2unix tool

使用dos2unix工具

dos2unix pickle.char

#1


10  

Probably you didn't open the file in binary mode when writing and/or reading the pickled data. In this case newline format conversion will occur, which can break the binary data.

在写入和/或读取pickle数据时,您可能没有以二进制模式打开文件。在这种情况下,将发生换行格式转换,这可能会破坏二进制数据。

To open a file in binary mode you have to provide "b" as part of the mode string:

要以二进制模式打开文件,您必须提供“b”作为模式字符串的一部分:

char_file = open('pickle.char', 'rb')

#2


8  

As mentioned by Adam, the problem is likely to be the newline format of the pickle file.

正如Adam所提到的,问题很可能是pickle文件的换行格式。

Unfortunately, the real problem is actually caused on save rather than load. This may be recoverable if you're using text mode pickles, rather than binary. Try opening the file in universal newline mode, which will cause python to guess what the right line-endings are ie:

不幸的是,真正的问题实际上是由保存而不是负载引起的。如果您使用的是文本模式pickles而不是binary,那么这可能是可恢复的。尝试以通用换行模式打开文件,这将导致python猜测正确的行结尾是什么,即:

char_file=open('filename.char','rU')

However, if you're using a binary format (cPickle.dump(file, 1)) you may have an unrecoverably corrupted pickle (even when loading in Windows) - if you're lucky and no \r\n characters show up then it may work, but as soon as this occurs you could end up with corrupted data, as there's no way to distinguish between a "real" \r\n code and one windows has inserted on seeing just \n.

但是,如果您使用的是二进制格式(cPickle.dump(file,1)),您可能会有一个不可恢复的损坏的泡菜(即使在Windows中加载) - 如果您很幸运并且没有\ r \ n字符出现,那么它可能会起作用,但是一旦发生这种情况你就可能最终得到损坏的数据,因为没有办法区分“真正的”\ r \ n代码和一个窗口已经插入只看到\ n。

The best way to handle things to be loaded in multiple platforms is to always save in binary mode. On your windows machine, when saving the pickle use:

处理要在多个平台上加载的东西的最佳方法是始终以二进制模式保存。在您的Windows机器上,保存泡菜时使用:

char_file = open('filename.char','wb')
cPickle.dumps(data, char_file)

#3


4  

Another way to get this error is to forget to close the output file after pickling. This can leave an incomplete file that fails in random ways during subsequent unpickling.

获得此错误的另一种方法是忘记在酸洗后关闭输出文件。这可能会留下一个不完整的文件,在随后的unpickling中会以随机方式失败。

#4


2  

self.file = open(self.file_name, 'w')

Should be:

self.file = open(self.file_name, 'wb')

In your createSaveFile function, to save the file in binary mode (rather than text mode). You should also make sure you open the file in binary mode as well (rb).

在createSaveFile函数中,以二进制模式(而不是文本模式)保存文件。您还应该确保以二进制模式打开文件(rb)。

If you don't use binary mode then Windows will convert all new-lines to \r\n and will effectively corrupt the file (at least as far as other OS's are concerned).

如果你不使用二进制模式,那么Windows会将所有新行转换为\ r \ n并且会有效地破坏文件(至少就其他操作系统而言)。

#5


0  

Use dos2unix tool

使用dos2unix工具

dos2unix pickle.char