Simple data storing in Python

心不动则不痛 提交于 2019-11-30 18:09:14

问题


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.

I'm sure python has library for doing such a task easily but so far all the approaches I have found seemed like it would have been sloppy to get it to work and I'm sure there is a better approach. So far I've tried:

  • the array.toFile() method but couldn't figure out how to get it to work with nested arrays of strings, it seemed geared towards integer data.
  • Lists and sets do not have a toFile method built in, so I would have had to parse and encode it manually.
  • CSV seemed like a good approach but this would also require manually parsing it, and did not allow me to simply append new lines at the end - so any new calls the the CSVWriter would overwrite the file existing data.

I'm really trying to avoid using databases (maybe SQLite but it seems a bit overkill) because I'm trying to develop this to have no software prerequisites besides Python.


回答1:


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




回答2:


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.




回答3:


http://docs.python.org/library/pickle.html




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/875228/simple-data-storing-in-python

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