我有很多数据文件(将近 150 个)是根据 Protocol Buffer 的 .proto 方案创建的二进制结构。是否有任何有效的解决方案如何在不丢失任何信息的情况下将所有文件合并为一个大的二进制数据文件?
请您参考如下方法:
如果您的方案允许,您可以合并现有数据。
方案
message People {
repeated Person person = 1;
}
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
现有数据
两个现有的二进制文件,每个包含一个Person。
person1.binperson2.bin
Python代码
import p_pb2
people = p_pb2.People()
people.person.add().ParseFromString(open("person1.bin", "rb").read())
people.person.add().ParseFromString(open("person2.bin", "rb").read())
with open("people.bin", "wb") as o:
o.write(people.SerializeToString())
合并数据
现在文件 people.bin 包含一个 People 实例,其中包括两个 Person 实例。
