Simple data storing in Python

怎甘沉沦 提交于 2019-12-01 00:01:40

Must the file be human readable? If not, shelve is really easy to use.

Hank Gay

In addition to pickle (mentioned above), there's json (built in to 2.6, available via simplejson before that), and marshal. Also, there's a reader in the same csv module the writer is in.

UPDATE: As S. Lott pointed out in a comment, there's also YAML, available via PyYAML, among others.

I'm looking for a simple solution using Python to store data as a flat file, such that each line is a string representation of an array that can be easily parsed.

Is the data only ever going to be parsed by Python programs? If not, then I'd avoid pickle et al (shelve and marshal) since they're very Python specific. JSON and YAML have the important advantage that parsers are easily available for most any language.

This solution at SourceForge uses only standard Python modules:

y_serial.py module :: warehouse Python objects with SQLite

"Serialization + persistance :: in a few lines of code, compress and annotate Python objects into SQLite; then later retrieve them chronologically by keywords without any SQL. Most useful "standard" module for a database to store schema-less data."

http://yserial.sourceforge.net

SQLite is not "overkill" at all -- you will be amazed how simple it is; plus it solves more general data persistance issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!