I am testing out Protocol buffers and trying to read a csv file, serialize it and write the output to a binary file and then read the binary file using ParseFromString. I am able to serialize and write the binary file however on reading it gives an index out of bounds exception or in the other case it just outputs the last line of the binary file, it skips everything before it.
My message is simple, it has two fields, time and metricusage.
syntax="proto3";
message excelData {
string time=1;
string meterusage=2;
}
The serialization and writing to a binary file code is below:
import metric_pb2
import sys
from csv import reader
excel_data=metric_pb2.excelData()
with open('out.bin', 'wb') as f:
with open('data.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
header = next(csv_reader)
if header != None:
for row in csv_reader:
excel_data.time=row[0]
excel_data.meterusage=row[1]
f.write(excel_data.SerializeToString())
f.close()
read_obj.close()
The troublesome part is below:
Approach 1: This only returns the last line of the binary file. It skips everything before it.
excel_data=metric_pb2.excelData()
with open('out.bin', 'rb') as f:
content=f.read()
excel_data.ParseFromString(content)
print(excel_data.time)
print(excel_data.meterusage)
Approach 2: If I read the serialized binary file like the csv file above it gives me an index out of bound error. My inclination is that maybe the binary file is byte data and does not contain string data types it is giving this error?
What's the correct way to read this binary file using message.ParseFromString() because reading it via a loop doesn't work, nor reading it as whole works? A snapshot of my created binary file is below:
out.bin
. It appears thatParseFromString
is grabbing the first|last record only and parsing that. You'll need to determine the length of your encoded message and iterate overout.bin
grabbing the correct number of bytes each time.