完全初学者尝试在 python 中创建平面文件数据库
试图保持愚蠢的简单。将 txt 文件移入和移出 python 列表是一个坏主意吗? txt 文件可能会包含大约 2-5k 条目。创建简单的平面文件数据库的首选方法是什么?
trying to keep it stupidly simple. Is it a bad idea to move a txt file into and out of a python list? the txt files will probably get to about 2-5k entries. what is the preferred method to create a simple flat file databse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是也可能不是一个坏主意。这取决于您想要实现的目标、您有多少内存以及这些行的平均大小。它还取决于您如何处理这些数据。也许逐行读取和处理文件是值得的?无论如何,数据库假设索引,你打算用列表做什么没有 索引 的字符串?例如,您无法有效地搜索它。
无论如何,如果您觉得需要一个数据库,请查看 SQLite 。它是一个小型嵌入式 SQL 服务器,用 C 与 Python 接口。它是稳定的并且被证明是有效的。例如,它在 iPhone 上的大量应用程序中使用。
It might or might not be a bad idea. It depends on what you are trying to achieve, how much memory you have and how big those lines are on average. It also depends on what you are doing with that data. Maybe it is worth it to read and process file line by line? In any case, database assumes indexes, what are you going to do with a list of strings without an index? You cannot search it efficiently, for example.
In any case, if you feel like you need a database, take a look at SQLite. It is a small embedded SQL server written in C with Python interface. It is stable and proven to work. For example, it is being used on iPhone in tons of applications.
如果您正在寻找一个非常简单的文件数据库,也许您应该查看 shelve 模块。用法示例:
If you're looking for a very simple file database, maybe you should look at the shelve module. Example usage: