Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load multiple dictionaries from a file

I have a file that looks like this:

{"cid" : "160686859281645","name" : "","s" : "JBLU131116P00011000","e" : "OPRA","p" : "-","c" : "-","b" : "3.60","a" : "3.80","oi" : "0","vol" : "-","strike" : "11.00","expiry" : "Nov 16, 2013"};

{"cid" : "721018656376031","name" : "","s" : "JBLU131116P00012000","e" : "OPRA","p" : "-","c" : "-","b" : "4.60","a" : "4.80","oi" : "0","vol" : "-","strike" : "12.00","expiry" : "Nov 16, 2013"};

How can I load these lines into Python so I can access the key:value pairs?

like image 681
namor Avatar asked Mar 24 '26 00:03

namor


1 Answers

Those look like JSON serialized objects (apart from the trailing ;). Assuming that they are one per line, you can load them with:

import json

yourData = []
with open("fileName.txt") as inputData:
    for line in inputData:
        try:
            yourData.append(json.loads(line.rstrip(';\n')))
        except ValueError:
            print "Skipping invalid line {0}".format(repr(line))

print yourData

If the JSON objects are not all one per line, you can read until you find a ; (outside a string literal) and process that with the same logic above, instead of reading one line at a time. If the file is small, you could even read it all in memory and split it.

Here we go:

>>> import json
>>> 
>>> yourData = []
>>> with open("fileName.txt") as inputData:
...     for line in inputData:
...         try:
...             yourData.append(json.loads(line.rstrip(';\n')))
...         except ValueError:
...             print "Skipping invalid line {0}".format(repr(line))
... 
Skipping invalid line '\n'
>>> print yourData
[{u'a': u'3.80', u'c': u'-', u'b': u'3.60', u'e': u'OPRA', u'name': u'', u'oi': u'0', u'cid': u'160686859281645', u'vol': u'-', u'expiry': u'Nov 16, 2013', u'p': u'-', u's': u'JBLU131116P00011000', u'strike': u'11.00'}, {u'a': u'4.80', u'c': u'-', u'b': u'4.60', u'e': u'OPRA', u'name': u'', u'oi': u'0', u'cid': u'721018656376031', u'vol': u'-', u'expiry': u'Nov 16, 2013', u'p': u'-', u's': u'JBLU131116P00012000', u'strike': u'12.00'}]
>>>
>>> import pprint
>>> pprint.pprint(yourData
... )
[{u'a': u'3.80',
  u'b': u'3.60',
  u'c': u'-',
  u'cid': u'160686859281645',
  u'e': u'OPRA',
  u'expiry': u'Nov 16, 2013',
  u'name': u'',
  u'oi': u'0',
  u'p': u'-',
  u's': u'JBLU131116P00011000',
  u'strike': u'11.00',
  u'vol': u'-'},
 {u'a': u'4.80',
  u'b': u'4.60',
  u'c': u'-',
  u'cid': u'721018656376031',
  u'e': u'OPRA',
  u'expiry': u'Nov 16, 2013',
  u'name': u'',
  u'oi': u'0',
  u'p': u'-',
  u's': u'JBLU131116P00012000',
  u'strike': u'12.00',
  u'vol': u'-'}]
like image 198
Stefano Sanfilippo Avatar answered Mar 26 '26 12:03

Stefano Sanfilippo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!