Python 中的简单数据存储
我正在寻找一种简单的解决方案,使用 Python 将数据存储为平面文件,这样每一行都是可以轻松解析的数组的字符串表示形式。
我确信 python 有可以轻松完成此类任务的库,但到目前为止,我发现的所有方法似乎都很难让它工作,我确信有更好的方法。 到目前为止,我已经尝试过:
- array.toFile() 方法,但无法弄清楚如何让它与嵌套的字符串数组一起使用,它似乎适合整数数据。
- 列表和集合没有内置的 toFile 方法,因此我必须手动对其进行解析和编码。
- CSV 似乎是一个很好的方法,但这也需要手动解析它,并且不允许我简单地在末尾附加新行 - 因此任何新调用 CSVWriter 都会覆盖文件现有数据。
我真的试图避免使用数据库(也许是 SQLite,但它似乎有点矫枉过正),因为我试图将其开发为除了 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
数据只能由 Python 程序解析吗? 如果没有,那么我会避免使用pickle等(shelve和marshal),因为它们是Python特定的。 JSON 和 YAML 具有重要的优势,即解析器可轻松用于大多数任何语言。
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.
SourceForge 的这个解决方案仅使用标准的 Python 模块:
y_serial.py module :: 使用 SQLite 仓库 Python 对象
“序列化 + 持久化 :: 用几行代码,将 Python 对象压缩并注释到 SQLite 中;然后通过关键字按时间顺序检索它们,而无需任何 SQL。用于存储无模式数据的数据库的最有用的“标准”模块。”
http://yserial.sourceforge.net
SQLite 根本不是“杀伤力大”——你会惊讶地发现很简单; 另外它还解决了更一般的数据持久性问题。
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.
除了
pickle
(上面提到的 ),有json
(内置于 2.6,可用通过之前的 simplejson ),以及marshal
。 此外,还有一个阅读器
与作者所在的相同csv
模块更新:如S. Lott 在评论中指出,还有 YAML,可通过 PyYAML 等获取。
In addition to
pickle
(mentioned above), there'sjson
(built in to 2.6, available via simplejson before that), andmarshal
. Also, there's areader
in the samecsv
module the writer is in.UPDATE: As S. Lott pointed out in a comment, there's also YAML, available via PyYAML, among others.
http://docs.python.org/library/pickle.html
http://docs.python.org/library/pickle.html
文件必须是人类可读的吗? 如果没有,shelve 确实很容易使用。
Must the file be human readable? If not, shelve is really easy to use.