1. <tfoot id='bUOrt'></tfoot>
          <bdo id='bUOrt'></bdo><ul id='bUOrt'></ul>
        <i id='bUOrt'><tr id='bUOrt'><dt id='bUOrt'><q id='bUOrt'><span id='bUOrt'><b id='bUOrt'><form id='bUOrt'><ins id='bUOrt'></ins><ul id='bUOrt'></ul><sub id='bUOrt'></sub></form><legend id='bUOrt'></legend><bdo id='bUOrt'><pre id='bUOrt'><center id='bUOrt'></center></pre></bdo></b><th id='bUOrt'></th></span></q></dt></tr></i><div id='bUOrt'><tfoot id='bUOrt'></tfoot><dl id='bUOrt'><fieldset id='bUOrt'></fieldset></dl></div>
        <legend id='bUOrt'><style id='bUOrt'><dir id='bUOrt'><q id='bUOrt'></q></dir></style></legend>

        <small id='bUOrt'></small><noframes id='bUOrt'>

      2. 在泡菜文件中保存和加载多个对象?

        Saving and loading multiple objects in pickle file?(在泡菜文件中保存和加载多个对象?)
      3. <legend id='9iJvJ'><style id='9iJvJ'><dir id='9iJvJ'><q id='9iJvJ'></q></dir></style></legend>
          <bdo id='9iJvJ'></bdo><ul id='9iJvJ'></ul>

            <tfoot id='9iJvJ'></tfoot>
            <i id='9iJvJ'><tr id='9iJvJ'><dt id='9iJvJ'><q id='9iJvJ'><span id='9iJvJ'><b id='9iJvJ'><form id='9iJvJ'><ins id='9iJvJ'></ins><ul id='9iJvJ'></ul><sub id='9iJvJ'></sub></form><legend id='9iJvJ'></legend><bdo id='9iJvJ'><pre id='9iJvJ'><center id='9iJvJ'></center></pre></bdo></b><th id='9iJvJ'></th></span></q></dt></tr></i><div id='9iJvJ'><tfoot id='9iJvJ'></tfoot><dl id='9iJvJ'><fieldset id='9iJvJ'></fieldset></dl></div>

          1. <small id='9iJvJ'></small><noframes id='9iJvJ'>

                    <tbody id='9iJvJ'></tbody>
                  本文介绍了在泡菜文件中保存和加载多个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类为游戏中的玩家服务,创建他们和其他东西.

                  I have a class that serves players in a game, creates them and other things.

                  我需要将这些播放器对象保存在一个文件中以供以后使用.我已经尝试过 pickle 模块,但我不知道如何保存多个对象并再次加载它们?有没有办法做到这一点,或者我应该使用其他类(如列表)并将我的对象保存和加载到列表中?

                  I need to save these player objects in a file to use it later. I've tried the pickle module but I don't know how to save multiple objects and again loading them? Is there a way to do that or should I use other classes such as lists and save and load my objects in a list?

                  有没有更好的办法?

                  推荐答案

                  使用列表、元组或字典是迄今为止最常见的方法:

                  Using a list, tuple, or dict is by far the most common way to do this:

                  import pickle
                  PIK = "pickle.dat"
                  
                  data = ["A", "b", "C", "d"]
                  with open(PIK, "wb") as f:
                      pickle.dump(data, f)
                  with open(PIK, "rb") as f:
                      print pickle.load(f)
                  

                  打印出来的:

                  ['A', 'b', 'C', 'd']
                  

                  然而,一个泡菜文件可以包含任意数量的泡菜.这是产生相同输出的代码.但请注意,它更难编写和理解:

                  However, a pickle file can contain any number of pickles. Here's code producing the same output. But note that it's harder to write and to understand:

                  with open(PIK, "wb") as f:
                      pickle.dump(len(data), f)
                      for value in data:
                          pickle.dump(value, f)
                  data2 = []
                  with open(PIK, "rb") as f:
                      for _ in range(pickle.load(f)):
                          data2.append(pickle.load(f))
                  print data2
                  

                  如果你这样做,你有责任知道你写出的文件中有多少泡菜.上面的代码通过首先挑选列表对象的数量来做到这一点.

                  If you do this, you're responsible for knowing how many pickles are in the file you write out. The code above does that by pickling the number of list objects first.

                  这篇关于在泡菜文件中保存和加载多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                    <tbody id='9ZJle'></tbody>
                1. <small id='9ZJle'></small><noframes id='9ZJle'>

                  <tfoot id='9ZJle'></tfoot>
                    <i id='9ZJle'><tr id='9ZJle'><dt id='9ZJle'><q id='9ZJle'><span id='9ZJle'><b id='9ZJle'><form id='9ZJle'><ins id='9ZJle'></ins><ul id='9ZJle'></ul><sub id='9ZJle'></sub></form><legend id='9ZJle'></legend><bdo id='9ZJle'><pre id='9ZJle'><center id='9ZJle'></center></pre></bdo></b><th id='9ZJle'></th></span></q></dt></tr></i><div id='9ZJle'><tfoot id='9ZJle'></tfoot><dl id='9ZJle'><fieldset id='9ZJle'></fieldset></dl></div>
                      • <bdo id='9ZJle'></bdo><ul id='9ZJle'></ul>

                          <legend id='9ZJle'><style id='9ZJle'><dir id='9ZJle'><q id='9ZJle'></q></dir></style></legend>