繁体   English   中英

在文件中存储大量自定义Python对象的最佳方法是什么?

[英]What is the best way to store big amount of custom Python objects in a file?

我需要一些帮助,以找到存储大量数据(1〜2GB)的最佳方法。 数据源是一个原始二进制文件,其中包含在两个设备之间交换的网络应用程序数据包。

数据包类由我自己在Python中定义(请参见下文)。

我想以这样一种方式存储对象,以便以后可以逐包读取文件,而不是逐字节读取文件

class AppPacket:
    def __init__(self, version=0, command=0, flags=0, seq=0, pldlen=0, pld=[]):
        self.Version = np.uint8(version)
        self.Command = np.uint8(command)
        self.Flags = np.uint16(flags)
        self.SequenceNumber = np.uint16(seq)
        self.PayloadLength = np.uint16(pldlen)
        self.Payload = np.uint8(pld)
        self.CRC8 = np.uint8(0)

逐字节读取数据并解析数据以重建每个数据包至少需要30分钟才能完成750MB。 我希望尽可能减少这个时间

如@Kris所建议,要关闭该主题,最好的方法是使用数据库。 由于Python提供了本机SQLite3模块,因此我选择将其与SQLite Studio一起用于数据库管理。

我使用executemany()语句和多线程来提高存储过程中的性能。

参见: https : //www.tutorialspoint.com/sqlite/sqlite_python.htm

谢谢你 :)

暂无
暂无

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

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