python中简单数据存储的最佳格式

时间:2021-07-23 05:00:52

As a relatively new programmer, I have several times encountered situations where it would be beneficial for me to read and assemble program data from an external source rather than have it written in the code. This is mostly the case when there are a large number of objects of the same type. In such scenarios, object definitions quickly take up a lot of space in the code and add unnecessary impediment to readability.

作为一个相对较新的程序员,我曾多次遇到这样的情况,即从外部源读取和汇编程序数据而不是将其写入代码中对我有益。当存在大量相同类型的对象时,大多数情况都是如此。在这种情况下,对象定义会迅速占用代码中的大量空间,并增加不必要的可读性障碍。

As an example, I've been working on text-based RPG, which has a large number of rooms and items of which to keep track. Even a few items and rooms leads to massive blocks of object creation code.

作为一个例子,我一直在研究基于文本的RPG,它有大量的房间和项目可以跟踪。即使是少数项目和房间也会导致大量的对象创建代码。

I think it would be easier in this case to use some format of external data storage, reading from a file. In such a file, items and rooms would be stored by name and attributes, so that they could parsed into an object with relative ease.

我认为在这种情况下使用某种格式的外部数据存储(从文件读取)会更容易。在这样的文件中,项目和房间将按名称和属性存储,以便它们可以相对容易地解析为对象。

What formats would be best for this? I feel a full-blown database such as SQL would add unnecessary bloat to a fairly light script. On the other hand, an easy method of editing this data is important, either through an external application, or another python script. On the lighter end of things, the few I heard most often mentioned are XML, JSON, and YAML.

什么格式最适合这个?我觉得像SQL这样的完整数据库会给相当轻松的脚本增加不必要的膨胀。另一方面,通过外部应用程序或其他python脚本,编辑此数据的简单方法很重要。在较轻松的事情上,我听到的最少提到的是XML,JSON和YAML。

From what I've seen, XML does not seem like the best option, as many seem to find it complex and difficult to work with effectively.

从我所看到的,XML似乎不是最好的选择,因为许多人似乎发现它复杂而且难以有效地工作。

JSON and YAML seem like either might work, but I don't know how easy it would be to edit either externally. Speed is not a primary concern in this case. While faster implementations are of course desirable, it is not a limiting factor to what I can use.

JSON和YAML似乎可能有用,但我不知道从外部编辑是多么容易。在这种情况下,速度不是主要关注点。虽然更快的实现当然是可取的,但它并不是我可以使用的限制因素。

I've looked around both here and via Google, and while I've seen quite a bit on the topic, I have not been able to find anything specifically helpful to me. Will formats like JSON or YAML be sufficient for this, or would I be better served with a full-blown database?

我在这里和谷歌都环顾四周,虽然我已经看过很多关于这个主题的内容,但我找不到任何对我有用的东西。像JSON或YAML这样的格式是否足够,或者我会更好地使用完整的数据库?

7 个解决方案

#1


5  

Though there are good answers here already, I would simply recommend JSON for your purposes for the sole reason that since you're a new programmer it will be the most straightforward to read and translate as it has the most direct mapping to native Python data types (lists [] and dictionaries {}). Readability goes a long way and is one of the tenets of Python programming.

虽然这里已有很好的答案,但我只是推荐JSON用于你的目的,因为你是一个新的程序员,它将是最直接的阅读和翻译,因为它有最直接的映射到本机Python数据类型(列出[]和字典{})。可读性有很长的路要走,也是Python编程的原则之一。

#2


3  

Well it depends on your use case.

那取决于你的用例。

If your file is relatively small and almost static, either YAML or JSON can serve the purpose. Check out What is the difference between YAML and JSON? When to prefer one over the other for more info

如果您的文件相对较小并且几乎是静态的,则YAML或JSON可以满足此目的。看看YAML和JSON有什么区别?何时更喜欢一个而不是另一个以获取更多信息

If your file is huge, or dynamic, or there will be some concurrency control involved, then you 'd better let a database to handle it.

如果您的文件很大或是动态的,或者涉及到一些并发控制,那么您最好让数据库来处理它。

#3


3  

I'm a big fan of using Python Pickles to store data in files.

我非常喜欢使用Python Pickles将数据存储在文件中。

A Pickle can serialize any kind of Python object properly, in particular "Complex" things -- any kind of Python Class, Functions -- any kind of Object at all!

Pickle可以正确地序列化任何类型的Python对象,特别是“复杂”的东西 - 任何类型的Python类,函数 - 任何类型的对象都可以!

It is not limited to the relatively simple structures like "lists", "dicts", "strings" and "numbers" available in data formats like JSON.

它不仅限于像JSON这样的数据格式中可用的相对简单的结构,如“list”,“dicts”,“strings”和“numbers”。

#4


2  

I've had similar needs for a couple applications and settled on JSON using jsonpickle.

我对几个应用程序有类似的需求,并使用jsonpickle确定了JSON。

To make the json output more human readable/editable I use these formatting settings:

为了使json输出更具人性化/可编辑性,我使用以下格式设置:

jsonpickle.set_encoder_options('simplejson', sort_keys=True, indent=4)

Then to encode / decode data:

然后编码/解码数据:

text = jsonpickle.encode(data)
...
data = jsonpickle.decode(text)

The nice thing about jsonpickle is it lets you store class objects without needing to manually convert everything to dicts (as you would if you used plain JSON). jsonpickle also includes hooks to let you define your own converters, if you need more control over how things get converted.

关于jsonpickle的好处是,它允许您存储类对象,而无需手动将所有内容转换为dicts(就像使用普通JSON一样)。 jsonpickle还包括钩子,让你可以定义自己的转换器,如果你需要更多地控制转换的方式。

Relational databases have their place of course, particularly for large multiplayer games; if a lot of your game logic involves frequent small updates to a huge number of objects the database approach is going win.

关系数据库当然占有一席之地,特别是对于大型多人游戏;如果您的许多游戏逻辑涉及对大量对象的频繁小更新,则数据库方法将获胜。

[Update] One further note, if you're going to be hand-editing json files a lot, make yourself a little json-checker script you can run on your edited files to find syntax errors, it'll save you a fair bit of time.

[更新]还有一点需要注意的是,如果你要手动编辑json文件,可以自己制作一个json-checker脚本,你可以在你编辑的文件上运行来查找语法错误,它会为你节省一些时间。时间

#5


1  

If you want editability, YAML is the best option of the ones you've named, because it doesn't have <> or {} required delimiters.

如果您想要可编辑性,YAML是您命名的最佳选项,因为它没有<>或{}所需的分隔符。

#6


1  

XML, JSON or YAML are more "loose" formats than what a relational database would give you. Relational databases are table-oriented and will impose some constraints in the way you store data.

XML,JSON或YAML是比关系数据库提供的更“松散”的格式。关系数据库是面向表的,会对存储数据的方式施加一些约束。

From your description, I'd stick to JSON or YAML. With them, you can express fairly complex object graphs (XML would be my option if you require more "formal" typing or schemas).

根据您的描述,我会坚持使用JSON或YAML。使用它们,您可以表达相当复杂的对象图(如果您需要更多“正式”输入或模式,XML将是我的选择)。

For reading or writing operations, you usually consider serialiszing/deserializing to/from objects (like http://docs.python.org/library/json.html does).

对于读取或写入操作,通常考虑对对象进行序列化/反序列化(如http://docs.python.org/library/json.html)。

#7


0  

I would be tempted to research a little into some GUI that could output graphviz (DOT format) with annotations, so you could create the rooms and links between them (a sort of graph). Then later, you might want another format to support heftier info.

我很想研究一些可以输出带注释的graphviz(DOT格式)的GUI,这样你就可以在它们之间创建房间和链接(一种图形)。然后,您可能想要另一种格式来支持更大的信息。

But should make it easy to create maps, links between rooms (containing items or traps etc..), and you could use common libraries to produce graphics of the maps in png or something.

但是应该可以轻松创建地图,房间之间的链接(包含项目或陷阱等),并且您可以使用公共库来生成png或其他地图的图形。

Just a random idea off the top of my head - feel free to ignore!

只是一个随意的想法 - 我可以随意忽略!

#1


5  

Though there are good answers here already, I would simply recommend JSON for your purposes for the sole reason that since you're a new programmer it will be the most straightforward to read and translate as it has the most direct mapping to native Python data types (lists [] and dictionaries {}). Readability goes a long way and is one of the tenets of Python programming.

虽然这里已有很好的答案,但我只是推荐JSON用于你的目的,因为你是一个新的程序员,它将是最直接的阅读和翻译,因为它有最直接的映射到本机Python数据类型(列出[]和字典{})。可读性有很长的路要走,也是Python编程的原则之一。

#2


3  

Well it depends on your use case.

那取决于你的用例。

If your file is relatively small and almost static, either YAML or JSON can serve the purpose. Check out What is the difference between YAML and JSON? When to prefer one over the other for more info

如果您的文件相对较小并且几乎是静态的,则YAML或JSON可以满足此目的。看看YAML和JSON有什么区别?何时更喜欢一个而不是另一个以获取更多信息

If your file is huge, or dynamic, or there will be some concurrency control involved, then you 'd better let a database to handle it.

如果您的文件很大或是动态的,或者涉及到一些并发控制,那么您最好让数据库来处理它。

#3


3  

I'm a big fan of using Python Pickles to store data in files.

我非常喜欢使用Python Pickles将数据存储在文件中。

A Pickle can serialize any kind of Python object properly, in particular "Complex" things -- any kind of Python Class, Functions -- any kind of Object at all!

Pickle可以正确地序列化任何类型的Python对象,特别是“复杂”的东西 - 任何类型的Python类,函数 - 任何类型的对象都可以!

It is not limited to the relatively simple structures like "lists", "dicts", "strings" and "numbers" available in data formats like JSON.

它不仅限于像JSON这样的数据格式中可用的相对简单的结构,如“list”,“dicts”,“strings”和“numbers”。

#4


2  

I've had similar needs for a couple applications and settled on JSON using jsonpickle.

我对几个应用程序有类似的需求,并使用jsonpickle确定了JSON。

To make the json output more human readable/editable I use these formatting settings:

为了使json输出更具人性化/可编辑性,我使用以下格式设置:

jsonpickle.set_encoder_options('simplejson', sort_keys=True, indent=4)

Then to encode / decode data:

然后编码/解码数据:

text = jsonpickle.encode(data)
...
data = jsonpickle.decode(text)

The nice thing about jsonpickle is it lets you store class objects without needing to manually convert everything to dicts (as you would if you used plain JSON). jsonpickle also includes hooks to let you define your own converters, if you need more control over how things get converted.

关于jsonpickle的好处是,它允许您存储类对象,而无需手动将所有内容转换为dicts(就像使用普通JSON一样)。 jsonpickle还包括钩子,让你可以定义自己的转换器,如果你需要更多地控制转换的方式。

Relational databases have their place of course, particularly for large multiplayer games; if a lot of your game logic involves frequent small updates to a huge number of objects the database approach is going win.

关系数据库当然占有一席之地,特别是对于大型多人游戏;如果您的许多游戏逻辑涉及对大量对象的频繁小更新,则数据库方法将获胜。

[Update] One further note, if you're going to be hand-editing json files a lot, make yourself a little json-checker script you can run on your edited files to find syntax errors, it'll save you a fair bit of time.

[更新]还有一点需要注意的是,如果你要手动编辑json文件,可以自己制作一个json-checker脚本,你可以在你编辑的文件上运行来查找语法错误,它会为你节省一些时间。时间

#5


1  

If you want editability, YAML is the best option of the ones you've named, because it doesn't have <> or {} required delimiters.

如果您想要可编辑性,YAML是您命名的最佳选项,因为它没有<>或{}所需的分隔符。

#6


1  

XML, JSON or YAML are more "loose" formats than what a relational database would give you. Relational databases are table-oriented and will impose some constraints in the way you store data.

XML,JSON或YAML是比关系数据库提供的更“松散”的格式。关系数据库是面向表的,会对存储数据的方式施加一些约束。

From your description, I'd stick to JSON or YAML. With them, you can express fairly complex object graphs (XML would be my option if you require more "formal" typing or schemas).

根据您的描述,我会坚持使用JSON或YAML。使用它们,您可以表达相当复杂的对象图(如果您需要更多“正式”输入或模式,XML将是我的选择)。

For reading or writing operations, you usually consider serialiszing/deserializing to/from objects (like http://docs.python.org/library/json.html does).

对于读取或写入操作,通常考虑对对象进行序列化/反序列化(如http://docs.python.org/library/json.html)。

#7


0  

I would be tempted to research a little into some GUI that could output graphviz (DOT format) with annotations, so you could create the rooms and links between them (a sort of graph). Then later, you might want another format to support heftier info.

我很想研究一些可以输出带注释的graphviz(DOT格式)的GUI,这样你就可以在它们之间创建房间和链接(一种图形)。然后,您可能想要另一种格式来支持更大的信息。

But should make it easy to create maps, links between rooms (containing items or traps etc..), and you could use common libraries to produce graphics of the maps in png or something.

但是应该可以轻松创建地图,房间之间的链接(包含项目或陷阱等),并且您可以使用公共库来生成png或其他地图的图形。

Just a random idea off the top of my head - feel free to ignore!

只是一个随意的想法 - 我可以随意忽略!