Pickle vs XML for file I / O. [英] Pickle vs XML for file I/O

查看:67
本文介绍了Pickle vs XML for file I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为经验和实践创建一个RPG。我已经完成了一个

字符创建模块,我正在试图弄清楚如何让

文件I / O工作。


我已经阅读了python新闻组,看起来搁置

可能不是出于各种原因的最佳选择。这导致我用b / b
尝试搞乱泡菜,但我不知道如何使用

类。我找到了很多使用pickle w / non-OOP代码的例子,但

没有显示如何使用它有类,子类等。我已经

阅读文档,但它对我来说并不是很好解释。


然后我开始思考也许我错了。我目前的

的想法是保存每个角色的实例,所以所有信息(名称,

技能,生命值等)都存储在一个地方。但是看看

OpenRPG让我觉得也许用XML来存储信息

会更好。每个角色都可以有一个单独的XML文件,但

如果需要很多NPC,我不知道它是如何工作的。


我猜猜我的问题是,让泡菜工作的好处是什么?
w /我的课程与将所有字符数据转换成XML并且只是将b / b
写入文件?由于大多数数据需要经常修改,例如b $ b。点数,是一种存储格式比

更好吗?您是否可以修改数据,如果它被腌制而无需支付它,更改数据,然后重新调整数据?

I''m creating an RPG for experience and practice. I''ve finished a
character creation module and I''m trying to figure out how to get the
file I/O to work.

I''ve read through the python newsgroup and it appears that shelve
probably isn''t the best option for various reasons. This lead me to
try messing w/ pickle, but I can''t figure out how to use it with
classes. I''ve found many examples of using pickle w/ non-OOP code but
nothing that shows how to use it w/ classes, subclasses, etc. I''ve
read the documentation but it doesn''t explain it well enough for me.

Then I started thinking perhaps I''m going about it wrong. My current
thought is to save an instance of each character so all the info (name,
skills, hit points, etc.) is stored in one place. But looking at
OpenRPG made me think that perhaps using XML to store the information
would be better. Each character could have a separate XML file, though
I don''t know how it would work if many NPC''s are required.

I guess my question is, what are the benifits of getting pickle to work
w/ my classes vs. converting all the character data into XML and just
writing that to a file? Since most of the data would need to be
modified often, e.g. hit points, is one storage format better than the
other? Can you even modify data if it''s been pickled w/o having to
unpickle it, change the data, then repickle it?

推荐答案

crystalattice写道:
crystalattice wrote:

我正在为经验和实践创建一个RPG。我已经完成了一个

字符创建模块,我正在试图弄清楚如何让

文件I / O工作。


我已经阅读了python新闻组,看起来搁置

可能不是出于各种原因的最佳选择。这导致我用b / b
尝试搞乱泡菜,但我不知道如何使用

类。我找到了很多使用pickle w / non-OOP代码的例子,但

没有显示如何使用它有类,子类等。我已经

阅读文档,但它对我来说并不是很好解释。


然后我开始思考也许我错了。我目前的

的想法是保存每个角色的实例,所以所有信息(名称,

技能,生命值等)都存储在一个地方。但是看看

OpenRPG让我觉得也许用XML来存储信息

会更好。每个角色都可以有一个单独的XML文件,但

如果需要很多NPC,我不知道它是如何工作的。


我猜猜我的问题是,让泡菜工作的好处是什么?
w /我的课程与将所有字符数据转换成XML并且只是将b / b
写入文件?由于大多数数据需要经常修改,例如b $ b。点数,是一种存储格式比

更好吗?您是否可以修改数据,如果它被腌制而无需支付它,更改数据,然后重新调整数据?
I''m creating an RPG for experience and practice. I''ve finished a
character creation module and I''m trying to figure out how to get the
file I/O to work.

I''ve read through the python newsgroup and it appears that shelve
probably isn''t the best option for various reasons. This lead me to
try messing w/ pickle, but I can''t figure out how to use it with
classes. I''ve found many examples of using pickle w/ non-OOP code but
nothing that shows how to use it w/ classes, subclasses, etc. I''ve
read the documentation but it doesn''t explain it well enough for me.

Then I started thinking perhaps I''m going about it wrong. My current
thought is to save an instance of each character so all the info (name,
skills, hit points, etc.) is stored in one place. But looking at
OpenRPG made me think that perhaps using XML to store the information
would be better. Each character could have a separate XML file, though
I don''t know how it would work if many NPC''s are required.

I guess my question is, what are the benifits of getting pickle to work
w/ my classes vs. converting all the character data into XML and just
writing that to a file? Since most of the data would need to be
modified often, e.g. hit points, is one storage format better than the
other? Can you even modify data if it''s been pickled w/o having to
unpickle it, change the data, then repickle it?



嗯,在课堂上使用泡菜并不是什么棘手的事:


|> import pickle

|> class foo:传递

|> f = foo()

|> pstr = pickle.dumps(f)

|> pstr

''(i__main__\\\
foo\\\
p0 \ n(dp1 \ nb。''

|> newf = pickle.loads(pstr)

|> newf

< __ main__.foo实例位于0xb664690c>


泡菜是简单而且应该工作开箱即用。在用我确定需要的东西之前,我不会因为XML而乱用




你和泡菜有什么麻烦?


和平,

~西蒙

Um, there''s nothing tricky to using pickle with classes:

|>import pickle
|>class foo: pass
|>f = foo()
|>pstr = pickle.dumps(f)
|>pstr
''(i__main__\nfoo\np0\n(dp1\nb.''
|>newf = pickle.loads(pstr)
|>newf
<__main__.foo instance at 0xb664690c>

Pickle is simple and should work "out-of-the-box". I wouldn''t mess
with XML until I was sure I needed it for something.

What kind of trouble were you having with pickle?

Peace,
~Simon


2006年7月31日星期一14:35:39 -1000,Simon Forman< ro ********* @ yahoo.com>

写道:
On Mon, 31 Jul 2006 14:35:39 -1000, Simon Forman <ro*********@yahoo.com>
wrote:

crystalattice写道:
crystalattice wrote:

>我正在创建一个RPG经验和实践。我已经完成了一个
角色创建模块,我正在试图弄清楚如何使文件I / O工作。

我已经阅读了python新闻组,看起来搁置
可能不是出于各种原因的最佳选择。这导致我尝试弄乱泡菜,但我不知道如何使用
类。我找到了许多使用pickle w / non-OOP代码的例子,但没有任何内容显示如何使用它有类,子类等。我已经阅读了文档,但它没有对我来说解释得不够好。

然后我开始思考也许我错了。我目前的想法是保存每个角色的实例,以便将所有信息(名称,技能,生命点等)存储在一个地方。但是看看OpenRPG让我觉得也许用XML来存储信息会更好。每个角色都可以有一个单独的XML文件,但是如果需要很多NPC,我不知道它是如何工作的。

我想我的问题是,什么是让咸菜工作的好处
w /我的课程与将所有角色数据转换成XML并只是将其写入文件?由于大多数数据需要经常修改,例如点数,是一种比其他存储格式更好的存储格式?您是否可以修改数据,如果它被腌制而无需取消它,更改数据,然后重新调整数据?
>I''m creating an RPG for experience and practice. I''ve finished a
character creation module and I''m trying to figure out how to get the
file I/O to work.

I''ve read through the python newsgroup and it appears that shelve
probably isn''t the best option for various reasons. This lead me to
try messing w/ pickle, but I can''t figure out how to use it with
classes. I''ve found many examples of using pickle w/ non-OOP code but
nothing that shows how to use it w/ classes, subclasses, etc. I''ve
read the documentation but it doesn''t explain it well enough for me.

Then I started thinking perhaps I''m going about it wrong. My current
thought is to save an instance of each character so all the info (name,
skills, hit points, etc.) is stored in one place. But looking at
OpenRPG made me think that perhaps using XML to store the information
would be better. Each character could have a separate XML file, though
I don''t know how it would work if many NPC''s are required.

I guess my question is, what are the benifits of getting pickle to work
w/ my classes vs. converting all the character data into XML and just
writing that to a file? Since most of the data would need to be
modified often, e.g. hit points, is one storage format better than the
other? Can you even modify data if it''s been pickled w/o having to
unpickle it, change the data, then repickle it?



嗯,在课堂上使用泡菜并不是很棘手:


|> import pickle

|> class foo:传递

|> f = foo()

|> pstr = pickle.dumps(f)

|> pstr

''(i__main__\\\
foo\\\
p0 \ n(dp1 \ nb。''

|> newf = pickle.loads(pstr)

|> newf

< __ main__.foo实例位于0xb664690c>


泡菜是简单而且应该工作开箱即用。在用我确定需要的东西之前,我不会因为XML而乱用




你和泡菜有什么麻烦?


和平,

~Simon


Um, there''s nothing tricky to using pickle with classes:

|>import pickle
|>class foo: pass
|>f = foo()
|>pstr = pickle.dumps(f)
|>pstr
''(i__main__\nfoo\np0\n(dp1\nb.''
|>newf = pickle.loads(pstr)
|>newf
<__main__.foo instance at 0xb664690c>

Pickle is simple and should work "out-of-the-box". I wouldn''t mess
with XML until I was sure I needed it for something.

What kind of trouble were you having with pickle?

Peace,
~Simon



这主要是事情的组合(我希望你能遵循我的逻辑)。

首先,要使用良好的编程习惯,我想实现一个

尝试/除了打开pickle文件的块。但我不知道是否

这个块应该包含在类之外,只包含在

基类中,或者基类和子类需要它;我倾向于将

作为一种全局方法放在课外。


当挑选一个类实例时,是否将pickle语句置于

class(比如说,在课程的最后)或者是实例是创建的
,例如test()方法?


我是否需要在字符生成模块中使用try / except块和pickle

语句,或者我应该有一个单独的

模块文件I / O,这样它打开新文件,调用

字符模块并创建一个新实例,然后将其腌制?


Plus,要修改类中的数据,我是否必须首先解开整个事物

,还是有办法在腌制时修改数据?实际上,

我想我可以回答最后一个问题:一个角色实例,已经创建了

,将留在内存中直到玩家退出,

字符死亡,或者不再需要。此时,

字符实例应该被腌制(如果需要)到磁盘; pickle

不应该在需要数据修改时使用,并且类

实例是有效,对吗?


我还记得在这里阅读一篇文章说,当用于课堂时,泡菜有时会出现问题

,这让我对整个班级腌制

实例犹豫不决。这就是为什么我认为XML可能更安全/更好。

-

基于Python的在线RPG开发基于殖民地海军陆战队来自

外国人: http://cmrpg.sourceforge.net


使用Opera革命性的电子邮件客户端: http ://www.opera.com/mail/




crystalattice写道:

crystalattice wrote:

>

另外,修改类
中的数据
>
Plus, to modify data in a class



我假设单词instance这里缺少...

I presume the word "instance" is missing here ...


我是否必须首先取消整个事物

或者有没有办法修改数据腌了?实际上,

我想我可以回答最后一个问题:一个角色实例,已经创建了

,将留在内存中直到玩家退出,

字符死亡,或者不再需要。此时,

字符实例应该被腌制(如果需要)到磁盘; pickle

不应该在需要数据修改时使用,并且类

实例是活动的,对吗?
do I have to unpickle the whole thing
first or is there a way to modify the data while it''s pickled? Actually,
I think I can answer that last question: a character instance, having
been created, will stay resident in memory until the player quits,
character dies, or otherwise is no longer needed. At that point the
character instance should be pickled (if necessary) to disk; pickle
shouldn''t be used while data modification is required and the class
instance is "active", correct?



pickle是对象及其内容的快照。如果真实的

风景发生变化,你需要拍另一张照片。


Unpickling从蓝图中创建另一个实例。您可以重复

这个以获得实例的多个不同副本。


无法进行部分取消。


如果可以,请使用cPickle;它要快得多。


HTH,

John

A pickle is a snapshot of an object and its contents. If the real
scenery changes, you need to take another photo.

Unpickling creates another instance from the blueprint. You can repeat
this to obtain multiple distinct copies of the instance.

Partial unpickling is not possible.

Use cPickle if you can; it''s much faster.

HTH,
John


这篇关于Pickle vs XML for file I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆