从文件加载多个词典

时间:2013-10-20 18:44:09

标签: python python-2.6

我有一个看起来像这样的文件:

{"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"};

如何将这些行加载到Python中,以便我可以访问key:value对?

1 个答案:

答案 0 :(得分:2)

那些看起来像JSON序列化对象(除了尾随;)。假设它们是每行一个,您可以使用以下命令加载它们:

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

如果JSON对象不是每行一个,那么您可以阅读,直到找到;(在字符串文字之外)并使用上面相同的逻辑处理它,而不是一次读取一行。如果文件很小,你甚至可以在内存中读取它并将其拆分。

我们走了:

>>> 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'-'}]