0

I have a pb file abc.pb file.

I need to read the file by retaining its format i.e. dont want to convert pb file to string and then again reconverting it.

Currently I m trying options with

    with open(data, "rb") as file_handle:
        data = file_handle.read()

But this converts that into bytes. How to read them as Message. Can someone please help?

1
  • Can you specify what you want to do with it? Is this a trained model in pb format or something else? Commented Jan 6, 2023 at 12:11

1 Answer 1

2

Protobuf is a binary encoding and -- generally -- you'll use a schema (types) to decode messages.

Protobuf types are described in proto files and compiled (using protoc) into langauge-specific stubs that can be used to encode|decode messages.

See Protocol Buffer Basics: Python which includes an example reading a message.

There are ways to decode binary files without a schema but it's more challenging.

So:

Either: See whether you can obtain the Python stubs for the pb file you're trying to decode

Or: If you have proto files, use protoc (see example above) to generate the Python stubs

Or: Use a tool like's Marc's (above) or some other library that will decode encoded arbitary messages for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.