简体   繁体   English

简单的数据存储在Python中

[英]Simple data storing in Python

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. 我正在寻找一个使用Python将数据存储为平面文件的简单解决方案,这样每行都是一个可以轻松解析的数组的字符串表示。

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. 我确信python有很容易完成这项任务的库,但到目前为止我发现的所有方法看起来都很邋to才能让它工作,我相信有更好的方法。 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. array.toFile()方法,但无法弄清楚如何使用嵌套的字符串数组,它似乎面向整数数据。
  • Lists and sets do not have a toFile method built in, so I would have had to parse and encode it manually. 列表和集合没有内置的toFile方法,因此我不得不手动解析和编码。
  • 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. CSV似乎是一个很好的方法,但这也需要手动解析它,并且不允许我在末尾添加新行 - 所以任何新的调用CSVWriter都会覆盖文件现有数据。

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. 我真的试图避免使用数据库(也许SQLite,但似乎有点矫枉过正),因为我正在尝试开发它,除了Python之外没有任何软件先决条件。

In addition to pickle ( mentioned above ), there's json (built in to 2.6, available via simplejson before that), and marshal . 除了pickle如上所述 )之外,还有json (内置于2.6,之前通过simplejson提供)和marshal Also, there's a reader in the same csv module the writer is in. 此外,作者所在的同一个csv模块中有一个reader

UPDATE: As S. Lott pointed out in a comment, there's also YAML, available via PyYAML , among others. 更新:正如S. Lott在评论中指出的,还有YAML,可通过PyYAML获得。

Must the file be human readable? 文件必须是人类可读的吗? If not, shelve is really easy to use. 如果没有, 搁置真的很容易使用。

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. 我正在寻找一个使用Python将数据存储为平面文件的简单解决方案,这样每行都是一个可以轻松解析的数组的字符串表示。

Is the data only ever going to be parsed by Python programs? 数据是否只能被Python程序解析? If not, then I'd avoid pickle et al (shelve and marshal) since they're very Python specific. 如果没有,那么我会避免pickle et al(搁置和编组),因为它们非常具有Python特性。 JSON and YAML have the important advantage that parsers are easily available for most any language. JSON和YAML具有重要的优势,即解析器可以轻松地用于大多数语言。

This solution at SourceForge uses only standard Python modules: SourceForge上的此解决方案仅使用标准Python模块:

y_serial.py module :: warehouse Python objects with SQLite y_serial.py module ::使用SQLite仓库Python对象

"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." “序列化+持久性::在几行代码中,将Python对象压缩并注释为SQLite;然后通过关键字按时间顺序检索它们,而不使用任何SQL。最有用的”标准“模块,用于存储无模式数据的数据库。”

http://yserial.sourceforge.net http://yserial.sourceforge.net

SQLite is not "overkill" at all -- you will be amazed how simple it is; SQLite根本不是“矫枉过正” - 你会惊讶它是多么简单; plus it solves more general data persistance issues. 加上它解决了更多的一般数据持久性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM