0

I have a file the output of it has serialized string from a protobuf in C++. I want to read it in python file. Here is the content of the file.

\n\013\010\202\331\242\233\006\020\200\256\263F\022$\022\024\r\255\314\355>\025\303\255&?\035<V\312=%\014H\223>\035\200\242\376>\"\007\022\005Pants\022\"\022\024\rmv\036?\025|\032\224>\0354z->%\334\3354?\035\341G\360>\"\005\022\003Man\022\"\022\024\r\332K\335>\025a\261\252>\035\\\3534>%z\330\"?\035\324\334\354>\"\005\022\003Man\022$\022\024\r-\202%?\025*\331$?\035\300\240\310=%\364\r\236>\035\2778\352>\"\007\022\005Pants\022\"\022\024\r^\350-?\025\246\3034?\035\240\250E>%h~\226>\035\256\301\277>\"\005\022\003Man\022\'\022\024\r\262\271\336>\025\364\261\261>\035T\3310>%E\344\035?\035\261l\253>\"\n\022\010Clothing\022\'\022\024\rs\255\037?\025\023\207\226>\035\3347\'>%\222\3721?\035m\364\247>\"\n\022\010Clothing\022\"\022\024\r0\341\337>\025\224\032\346>\035\354q%>%D%m>\035\273\336\240>\"\005\022\003Top\022$\022\024\r0\341\337>\025\224\032\346>\035\354q%>%D%m>\035\036\035\226>\"\007\022\005Shirt\022\"\022\024\r\245G!?\025e\340\314>\0354\222 >%;\003\213>\035v2\225>\"\005\022\003Top\022%\022\024\r^\350-?\025\246\3034?\035\240\250E>%h~\226>\035*\204\217>\"\010\022\006Person\022&\022\024\rB\264!?\025\016r\314>\035\354t\036>%\260\373\213>\035\037/\217>\"\t\022\007T-shirt\022%\022\024\rJp\325=\025\212\260\241>\035V\361\344=%\224\036i>\035K\253\213>\"\010\022\006Person\022\'\022\024\r\307{\262>\025\332\360\"<\035\214\307\252=%\027hP>\035\033D\211>\"\n\022\010Lighting\022$\022\024\r\245G!?\025e\340\314>\0354\222 >%;\003\213>\0352t\207>\"\007\022\005Shirt\022\'\022\024\r\222\034\207<\025\037`\266>\035D\351\254=%\262BN>\0355\014\207>\"\n\022\010Lighting\022\"\022\024\r\370Q\330=\025NH\247>\035\354;\260=%\270gQ>\035\215\304\203>\"\005\022\003Man\0220\022\024\r\3123\217<\025\272\252#?\035\250\340\244>%z\257\267>\035\217\353~>\"\023\022\021Electronic device\022\'\022\024\r\330>\340>\025R\352\345>\035(\000%>%l\255l>\035\265\002~>\"\n\022\010Clothing\022$\022\024\r\302\3779>\025\206)\244>\035|\337\342=%\360jK>\035\244t{>\"\007\022\005Table\022%\022\024\rmv\036?\025|\032\224>\0354z->%\334\3354?\035\342E{>\"\010\022\006Person\022\'\022\024\r \323\330=\025\334\200\262>\035d\307\267=%\250._>\035-\224u>\"\n\022\010Clothing\022\'\022\024\re\017/?\025,\240;?\035`!\032>%P\333\210>\035\221up>\"\n\022\010Clothing\022\'\022\024\r\007\315$?\025\306E%?\035\240\312\307=%\362\014\237>\035\203)d>\"\n\022\010Clothing\022$\022\024\r-\202%?\025*\331$?\035\300\240\310=%\364\r\236>\035\010\261a>\"\007\022\005Jeans\022$\022\024\rk\247\355>\025NT&?\035\2240\313=%\272\272\223>\035\243\227a>\"\007\022\005Jeans\022\'\022\024\r\245G!?\025e\340\314>\0354\222 >%;\003\213>\035@_`>\"\n\022\010Clothing\0220\022\024\rF\'\027?\025dn}>\035\360J\232=%l\244;>\035m%\\>\"\023\022\021Electronic device\022,\022\024\r\222\034\207<\025\037`\266>\035D\351\254=%\262BN>\035F\276R>\"\017\022\rLight fixture\022(\022\024\r\247s=>\025\337a\251>\035\216\365\321=%zY\r>\035LIN>\"\013\022\tTable top

If I take the above string and assign it as a binary string, I am able to Parse it and interpret values correctly.

object_reader = objreader.ObjectDetectionPredictionResult()

orig = b'\n\013\010\202\331\242\233\006\020\200\256\263F\022$\022\024\r\255\314\355>\025\303\255&?\035<V\312=%\014H\223>\035\200\242\376>\"\007\022\005Pants\022\"\022\024\rmv\036?\025|\032\224>\0354z->%\334\3354?\035\341G\360>\"\005\022\003Man\022\"\022\024\r\332K\335>\025a\261\252>\035\\\3534>%z\330\"?\035\324\334\354>\"\005\022\003Man\022$\022\024\r-\202%?\025*\331$?\035\300\240\310=%\364\r\236>\035\2778\352>\"\007\022\005Pants\022\"\022\024\r^\350-?\025\246\3034?\035\240\250E>%h~\226>\035\256\301\277>\"\005\022\003Man\022\'\022\024\r\262\271\336>\025\364\261\261>\035T\3310>%E\344\035?\035\261l\253>\"\n\022\010Clothing\022\'\022\024\rs\255\037?\025\023\207\226>\035\3347\'>%\222\3721?\035m\364\247>\"\n\022\010Clothing\022\"\022\024\r0\341\337>\025\224\032\346>\035\354q%>%D%m>\035\273\336\240>\"\005\022\003Top\022$\022\024\r0\341\337>\025\224\032\346>\035\354q%>%D%m>\035\036\035\226>\"\007\022\005Shirt\022\"\022\024\r\245G!?\025e\340\314>\0354\222 >%;\003\213>\035v2\225>\"\005\022\003Top\022%\022\024\r^\350-?\025\246\3034?\035\240\250E>%h~\226>\035*\204\217>\"\010\022\006Person\022&\022\024\rB\264!?\025\016r\314>\035\354t\036>%\260\373\213>\035\037/\217>\"\t\022\007T-shirt\022%\022\024\rJp\325=\025\212\260\241>\035V\361\344=%\224\036i>\035K\253\213>\"\010\022\006Person\022\'\022\024\r\307{\262>\025\332\360\"<\035\214\307\252=%\027hP>\035\033D\211>\"\n\022\010Lighting\022$\022\024\r\245G!?\025e\340\314>\0354\222 >%;\003\213>\0352t\207>\"\007\022\005Shirt\022\'\022\024\r\222\034\207<\025\037`\266>\035D\351\254=%\262BN>\0355\014\207>\"\n\022\010Lighting\022\"\022\024\r\370Q\330=\025NH\247>\035\354;\260=%\270gQ>\035\215\304\203>\"\005\022\003Man\0220\022\024\r\3123\217<\025\272\252#?\035\250\340\244>%z\257\267>\035\217\353~>\"\023\022\021Electronic device\022\'\022\024\r\330>\340>\025R\352\345>\035(\000%>%l\255l>\035\265\002~>\"\n\022\010Clothing\022$\022\024\r\302\3779>\025\206)\244>\035|\337\342=%\360jK>\035\244t{>\"\007\022\005Table\022%\022\024\rmv\036?\025|\032\224>\0354z->%\334\3354?\035\342E{>\"\010\022\006Person\022\'\022\024\r \323\330=\025\334\200\262>\035d\307\267=%\250._>\035-\224u>\"\n\022\010Clothing\022\'\022\024\re\017/?\025,\240;?\035`!\032>%P\333\210>\035\221up>\"\n\022\010Clothing\022\'\022\024\r\007\315$?\025\306E%?\035\240\312\307=%\362\014\237>\035\203)d>\"\n\022\010Clothing\022$\022\024\r-\202%?\025*\331$?\035\300\240\310=%\364\r\236>\035\010\261a>\"\007\022\005Jeans\022$\022\024\rk\247\355>\025NT&?\035\2240\313=%\272\272\223>\035\243\227a>\"\007\022\005Jeans\022\'\022\024\r\245G!?\025e\340\314>\0354\222 >%;\003\213>\035@_`>\"\n\022\010Clothing\0220\022\024\rF\'\027?\025dn}>\035\360J\232=%l\244;>\035m%\\>\"\023\022\021Electronic device\022,\022\024\r\222\034\207<\025\037`\266>\035D\351\254=%\262BN>\035F\276R>\"\017\022\rLight fixture\022(\022\024\r\247s=>\025\337a\251>\035\216\365\321=%zY\r>\035LIN>\"\013\022\tTable top'

object_reader.ParseFromString(orig)
print(object_reader)

If the above string is in a file and I try and read it is as binary file and parse it, I get an error.

with open("./payload", "rb") as fd:
    val = fd.read()

object_reader.ParseFromString(val)
print(object_reader)

Error:

google.protobuf.message.DecodeError: Error parsing message

On looking further it seems while reading the file in binary format it is adding extra '\' escape characters. Which looks like below.

b'\\n\\013\\010\\202\\331\\242\\233\\006\\020\\200\\256\\263F\\022$\\022\\024\\r\\255\\314\\355>\\025\\303\\255&?\\035<V\\312=%\\014H\\223>\\035\\200\\242\\376>\\"\\007\\022\\005Pants\\022\\"\\022\\024\\rmv\\036?\\025|\\032\\224>\\0354z->%\\334\\3354?\\035\\341G\\360>\\"\\005\\022\\003Man\\022\\"\\022\\024\\r\\332K\\335>\\025a\\261\\252>\\035\\\\\\3534>%z\\330\\"?\\035\\324\\334\\354>\\"\\005\\022\\003Man\\022$\\022\\024\\r-\\202%?\\025*\\331$?\\035\\300\\240\\310=%\\364\\r\\236>\\035\\2778\\352>\\"\\007\\022\\005Pants\\022\\"\\022\\024\\r^\\350-?\\025\\246\\3034?\\035\\240\\250E>%h~\\226>\\035\\256\\301\\277>\\"\\005\\022\\003Man\\022\\\'\\022\\024\\r\\262\\271\\336>\\025\\364\\261\\261>\\035T\\3310>%E\\344\\035?\\035\\261l\\253>\\"\\n\\022\\010Clothing\\022\\\'\\022\\024\\rs\\255\\037?\\025\\023\\207\\226>\\035\\3347\\\'>%\\222\\3721?\\035m\\364\\247>\\"\\n\\022\\010Clothing\\022\\"\\022\\024\\r0\\341\\337>\\025\\224\\032\\346>\\035\\354q%>%D%m>\\035\\273\\336\\240>\\"\\005\\022\\003Top\\022$\\022\\024\\r0\\341\\337>\\025\\224\\032\\346>\\035\\354q%>%D%m>\\035\\036\\035\\226>\\"\\007\\022\\005Shirt\\022\\"\\022\\024\\r\\245G!?\\025e\\340\\314>\\0354\\222 >%;\\003\\213>\\035v2\\225>\\"\\005\\022\\003Top\\022%\\022\\024\\r^\\350-?\\025\\246\\3034?\\035\\240\\250E>%h~\\226>\\035*\\204\\217>\\"\\010\\022\\006Person\\022&\\022\\024\\rB\\264!?\\025\\016r\\314>\\035\\354t\\036>%\\260\\373\\213>\\035\\037/\\217>\\"\\t\\022\\007T-shirt\\022%\\022\\024\\rJp\\325=\\025\\212\\260\\241>\\035V\\361\\344=%\\224\\036i>\\035K\\253\\213>\\"\\010\\022\\006Person\\022\\\'\\022\\024\\r\\307{\\262>\\025\\332\\360\\"<\\035\\214\\307\\252=%\\027hP>\\035\\033D\\211>\\"\\n\\022\\010Lighting\\022$\\022\\024\\r\\245G!?\\025e\\340\\314>\\0354\\222 >%;\\003\\213>\\0352t\\207>\\"\\007\\022\\005Shirt\\022\\\'\\022\\024\\r\\222\\034\\207<\\025\\037`\\266>\\035D\\351\\254=%\\262BN>\\0355\\014\\207>\\"\\n\\022\\010Lighting\\022\\"\\022\\024\\r\\370Q\\330=\\025NH\\247>\\035\\354;\\260=%\\270gQ>\\035\\215\\304\\203>\\"\\005\\022\\003Man\\0220\\022\\024\\r\\3123\\217<\\025\\272\\252#?\\035\\250\\340\\244>%z\\257\\267>\\035\\217\\353~>\\"\\023\\022\\021Electronic device\\022\\\'\\022\\024\\r\\330>\\340>\\025R\\352\\345>\\035(\\000%>%l\\255l>\\035\\265\\002~>\\"\\n\\022\\010Clothing\\022$\\022\\024\\r\\302\\3779>\\025\\206)\\244>\\035|\\337\\342=%\\360jK>\\035\\244t{>\\"\\007\\022\\005Table\\022%\\022\\024\\rmv\\036?\\025|\\032\\224>\\0354z->%\\334\\3354?\\035\\342E{>\\"\\010\\022\\006Person\\022\\\'\\022\\024\\r \\323\\330=\\025\\334\\200\\262>\\035d\\307\\267=%\\250._>\\035-\\224u>\\"\\n\\022\\010Clothing\\022\\\'\\022\\024\\re\\017/?\\025,\\240;?\\035`!\\032>%P\\333\\210>\\035\\221up>\\"\\n\\022\\010Clothing\\022\\\'\\022\\024\\r\\007\\315$?\\025\\306E%?\\035\\240\\312\\307=%\\362\\014\\237>\\035\\203)d>\\"\\n\\022\\010Clothing\\022$\\022\\024\\r-\\202%?\\025*\\331$?\\035\\300\\240\\310=%\\364\\r\\236>\\035\\010\\261a>\\"\\007\\022\\005Jeans\\022$\\022\\024\\rk\\247\\355>\\025NT&?\\035\\2240\\313=%\\272\\272\\223>\\035\\243\\227a>\\"\\007\\022\\005Jeans\\022\\\'\\022\\024\\r\\245G!?\\025e\\340\\314>\\0354\\222 >%;\\003\\213>\\035@_`>\\"\\n\\022\\010Clothing\\0220\\022\\024\\rF\\\'\\027?\\025dn}>\\035\\360J\\232=%l\\244;>\\035m%\\\\>\\"\\023\\022\\021Electronic device\\022,\\022\\024\\r\\222\\034\\207<\\025\\037`\\266>\\035D\\351\\254=%\\262BN>\\035F\\276R>\\"\\017\\022\\rLight fixture\\022(\\022\\024\\r\\247s=>\\025\\337a\\251>\\035\\216\\365\\321=%zY\\r>\\035LIN>\\"\\013\\022\\tTable top:'

I am looking for some help in reading the above file while escaping the extra escape characters. Or any other ways of reading file and loading into protobuf to parse the values.

0 Answers0