protobuf和csv互相转换具体代码
时间: 2023-03-14 11:48:52 浏览: 193
可以使用Python来实现Protobuf和CSV之间的互相转换,具体代码如下: import protobuf
import csv
# 读取Protobuf文件
with open('sample.pb', 'rb') as f:
data = protobuf.load(f)
# 将Protobuf数据转换为CSV
with open('sample.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow([field.name for field in data.DESCRIPTOR.fields])
for row in data:
writer.writerow([getattr(row, field.name) for field in row.DESCRIPTOR.fields])
# 读取CSV文件
with open('sample.csv', 'r') as f:
reader = csv.reader(f)
fields = next(reader)
data = protobuf.Message('sample', fields)
for row in reader:
data.append(row)
# 将CSV数据转换为Protobuf
with open('sample.pb', 'wb') as f:
protobuf.dump(data, f)
阅读全文