3

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?

4

1 回答 1

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'-'}]
于 2013-10-20T18:47:52.923 回答