-1

I am trying to decode and process Binary data file, following is a data format input:9,data:443,gps:3 and has more data in the same fashion, [key:value] format.

basically, I need to create a dictionary of the file to process it later.

input:b'input:9,data:443,gps:3'

Desired output:{'input': '9', 'data': '443', 'gps': '3'}

  • please provide desired output – AzamAbbasi Sep 02 '19 at 13:09
  • id doesn't look like binary bytes data but normal text data and you can use normal string functions to split it - `split(',')` and `split(':')` – furas Sep 02 '19 at 13:23
  • Where did you get the information that the data format is in the form `input:9,data:443,gps:3`? The original description might be more understandable. – alexis Sep 02 '19 at 15:31

3 Answers3

1

Your input data is bytes(sequence of bytes). To convert it to str object you can use bytes.decode(). Than you can work with data lie with sting and split it by , and :. Code:

inp = b"input:9,data:443,gps:3"
out = dict(s.split(":") for s in inp.decode().split(","))
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • m getting this error : UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 337: invalid start byte @Olivn – ZombieCoder8 Sep 02 '19 at 13:39
  • @ZombieCoder8, 0xb5 is not valid utf-8 charcode. You can provide correct encoding as argument of `decode()` function – Olvin Roght Sep 02 '19 at 13:41
  • some of the file is following format \x8a\x03C#\xdbVcFZp\xb7\t\xc1\x95>=p6\xd8\xc8\xf3C\x1c\x0bv\x81\x0bn\xbd\xd6\x92_k\xf2\xd8D\xb8P6\x93a\xf5\xa8J\x9d\xbf4\xc9p\\\xd1\xa7\x9a\xbc\x1e\x85\xe9\xea\xd6X\xfe\xd46owZ+\xbcg\xb5\xd0\r#\xbb\xab4U\xe7\xd6\x11\x10W\xf6\x0bl\xb7\xd4\xfeM\x990\xfe\x85\xac\x1c:\x0b\xb3\x9dS%o\t@\xe1\xb6\x7f\xe0\x10G\xe0c\xd7,\xf1\x87\xf9\x8d\xef@\x9d\x92\xf6\x1c\xd5Z\xeb\x860Nk – ZombieCoder8 Sep 02 '19 at 13:47
  • @ZombieCoder8, is it text? or encoded text? or waht is this? It's not obvious from data you've posted. – Olvin Roght Sep 02 '19 at 13:49
  • sorry I am new to programming, the file is encoded. – ZombieCoder8 Sep 02 '19 at 13:52
  • @ZombieCoder8, I don't know, you should work on it. Or ask another question with more details. – Olvin Roght Sep 02 '19 at 15:21
  • Well, since the OP accepted the answer, I assumed the solution worked. @Zombie, Un-accept the answer since it is based on an inaccurate interpretation of your question. – alexis Sep 02 '19 at 15:30
  • @alexis, my answer based on provided input and output. Or I should answer few times every time question will be changed? – Olvin Roght Sep 02 '19 at 15:36
  • @Olvin you can ask that on meta. But in this case the question was always about *binary bytes*, and you guessed (reasonably but incorrectly, it turned out) that it was not actually a binary file. So don't blame the OP. – alexis Sep 03 '19 at 07:24
0

The string input:9,data:443,gps:3 is text, not binary data, so I am going to guess that it is a format template, not a sample of the file contents. This format would mean that your file has an "input" field that is 9 bytes long, followed by 443 bytes of "data", followed by a 3-byte "gps" value. This description does not specify the types of the fields, so it is incomplete; but it's a start.

The Python tool for structured binary files is the module struct. Here's how to extract the three fields as bytes objects:

import struct

with open("some_file.bin", "rb") as binfile:
    content = binfile.read()
    input_, data, gps = struct.unpack("9s443s3s", content)

The function struct.unpack provides many other formats besides s; this is just an example. But there is no specifier for plain text strings, so if input_ is a text string, the next step is to convert it:

    input_ = input_.decode("ascii")   # or other encoding

Since you ask for a dictionary of the results, here is one way:

result = { "input":input_, "data": data, "gps": gps }
alexis
  • 48,685
  • 16
  • 101
  • 161
-1

Solution using eval:

inp = b"input:9,data:443,gps:3"
out = eval(b'dict(%s)' % inp.replace(b':', b'='))
dtrckd
  • 657
  • 7
  • 17