<p>我有一个从pickle文件导入数据的应用程序。它在Windows下运行得很好,但是Mac和Linux的行为很奇怪。</p>
<p>在OSX中,pickled文件(文件扩展名“.char”)不可用作选择,除非我将文件类型设置为*.*。然后,如果我选择一个扩展名为.char的文件,它将不会加载,并给出错误</p>
<blockquote>
<pre><code>unpickle_file = cPickle.load(char_file)
</code></pre>
<p>ValueError: could not convert string to float</p>
</blockquote>
<p>但是,如果我创建了一个没有.char扩展名的文件,那么该文件将很好地加载。</p>
<p>在Linux中,当我使用“文件打开”对话框时,我的pickled文件不可见,无论它们是否具有文件扩展名。不过,我可以在鹦鹉螺或海豚下面看到它们。不过,我的应用程序根本不存在它们。</p>
<hr/>
<p><strong>编辑</strong>下面是保存代码:</p>
<pre><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()
</code></pre>
<p>以下是开放代码:</p>
<pre><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()
</code></pre>