Optimal format for easy data storage in python

As a relatively new programmer, I have come across several situations where it would be useful for me to read and collect program data from an external source, rather than writing in code. This is mainly 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 an unnecessary obstacle to readability.

As an example, I worked on a text RPG in which there are a large number of rooms and elements that need to be tracked. Even a few objects and rooms lead to massive blocks of code to create objects.

I think that in this case it would be easier to use some format of an external data store, counting from a file. In such a file, elements and rooms will be stored by name and attributes so that they can be easily disassembled in the object.

What formats would be better for this? I feel that a full-blown database such as SQL will add unnecessary bloat to a rather lightweight script. On the other hand, a simple way to edit this data is important either through an external application or in another python script. The lighter end of the things I mentioned most often include XML, JSON, and YAML.

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

JSON and YAML seem like they can work, but I don't know how easy it would be to edit either externally. Speed ​​in this case is not the main task. Although faster implementations are certainly desirable, this is not a limiting factor of what I can use.

I looked through both here and through Google, and although I am pretty much versed in this topic, I could not find anything specific for me. Will there be formats such as JSON or YAML for this, or am I better off serving a full-blown database?

+5
source share
7 answers

, JSON , , , , Python ( [] {}). Python.

+5

, .

, YAML JSON . YAML JSON?

, - concurrency, .

+3

Python Pickles .

A Pickle Python , "" - Python, - !

, "", "dicts", "string" "numbers", , JSON.

+3

JSON jsonpickle.

json /, :

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

/ :

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

jsonpickle - dicts ( JSON). jsonpickle , , , .

, , ; , .

[] : json , json-checker script, , , .

+2

, YAML - , , < > {} .

+1

XML, JSON YAML "" , , . .

JSON YAML. (XML , "" ).

/ / (, http://docs.python.org/library/json.html).

+1

I would be tempted to learn a little about some graphical interface that graphviz (DOT format) could display with annotations so that you could create rooms and links between them (a kind of graph). You may then need a different format to support more details.

But it should be easier for you to create maps, links between rooms (containing objects or traps, etc.), and you could use shared libraries to create graphics for maps in png or something like that.

Just a random idea from my head - feel free to ignore!

0
source

All Articles