0

I have a binary file that contains some text with a structure on a it. Here's a sample with some lines:

...    
sCoilSelectMeas.aRxCoilSelectData[0].asList[0].lElementSelected  =  1
sCoilSelectMeas.aRxCoilSelectData[0].asList[0].lMuxChannelConnected  =  8
sRXSPEC.lGain    =  1
sRXSPEC.asNucleusInfo[0].tNucleus    =  ""1H""
sRXSPEC.asNucleusInfo[0].lCoilSelectIndex    =  0
...

I've parsed this structure into a Python OrderedDict as an intermediate step, using the following function:

from collections import OrderedDict
def parse_x(binfile):
    result = OrderedDict()
    with open(binfile, 'rb') as openfile:
        rx = re.compile(b'(.*)\t = \t(.*)\n')
        for line in openfile:
            m = rx.match(line)
            if m:
                result[m.group(1).decode('utf-8')] = m.group(2).decode('utf-8')
    return result

so that now I have said OrderedDict with the following:

...    
('sCoilSelectMeas.aRxCoilSelectData[0].asList[0].lElementSelected', '1'),
('sCoilSelectMeas.aRxCoilSelectData[0].asList[0].lMuxChannelConnected', '8'),
('sRXSPEC.lGain', '1'),
('sRXSPEC.asNucleusInfo[0].tNucleus', '""1H""'),
('sRXSPEC.asNucleusInfo[0].lCoilSelectIndex', '0'),
...

How could I parse this OrderedDict into a Python object so that I could access fields as I would in the original structure? i.e.:

In[1]: sCoilSelectMeas.aRxCoilSelectData[0].asList[0].lElementSelected 
Out[1]: 1
Daniel
  • 11,332
  • 9
  • 44
  • 72
  • Looks like a file of code in some language. If not for the `""1H""` which isn't valid Python code, I would've said to just throw `eval` at it. – TigerhawkT3 Oct 25 '16 at 00:09
  • apparently all _strings_ are wrapped in double quotes. And there are also some hex values such as `ucScanRegionPosValid = 0x1` – Daniel Oct 25 '16 at 00:14
  • Sorry, `exec`, not `eval`. Anyway, it looks like your easiest options are to either clean up those double-quoted strings and `exec` the whole thing or write the file in a tidier way. – TigerhawkT3 Oct 25 '16 at 00:16
  • Wow. What a headache. Anyway, if you just want to access dictionary values like object attributes, there is [this](http://stackoverflow.com/a/14620633/5014455) hackey solution. There might be third-party libraries that do this better, too. – juanpa.arrivillaga Oct 25 '16 at 00:18
  • Unfortunately I can't do anything about the file. It's written by proprietary software I don't have access to. I tried `exec`, but it gives me `NameError` if it finds an object that isn't defined yet. E.g.: `exec('sCoilSelectUI.dOverallImageScaleFactor=1.0')`==> `NameError: sCoilSelectUI not defined.` – Daniel Oct 25 '16 at 00:19
  • Is the index always zero? e.g. `sRXSPEC.asNucleusInfo[0]`? That would greatly simplify things. Either way, it's probably going to end up being a house of cards. – juanpa.arrivillaga Oct 25 '16 at 00:22
  • @juanpa.arrivillaga most of them are only 0, but some go up to 127. – Daniel Oct 25 '16 at 00:23
  • Well I suppose you can just use a dict. – juanpa.arrivillaga Oct 25 '16 at 00:55

0 Answers0