读取日志文件将字典作为字典放入python中

时间:2017-02-09 09:58:13

标签: python dictionary

我有一个日志文件,其格式为以下格式:

{"headers": {"ai5": "df9752bca47ff5efb3dd85291eaca875", "debug": null, "random": null, "sdkv": "7.5.1"}, "post": {"event": "ggstop", "ts": "1462759197931"}, "params": {}, "bottle": {"timestamp": "2016-05-09 02:00:00.690341", "game_id": "10655437"}}

{"headers": {"ai5": "df9752bca47ff5efb3dd85291eaca875", "debug": null, "random": null, "sdkv": "7.5.1"}, "post": {"event": "ggstart", "ts": "1462759198073"}, "params": {}, "bottle": {"timestamp": "2016-05-09 02:00:00.694609", "game_id": "10655437"}}

{"headers": {"ai5": "aca6d26817f1898723ff6c7abe3ee9dd", "debug": null, "random": null, "sdkv": "7.5.1"}, "post": {"event": "ggstop", "ts": "1462759198255"}, "params": {}, "bottle": {"timestamp": "2016-05-09 02:00:00.697425", "game_id": "10655437"}}

我想把它作为字典读入python。 从每一行我想要“ai5”,“timestamp”,“event”和“ts”。

我试过了:

my_dict = {}

with open("sample.log", 'r') as f:
    for line in f:
        items = line.split()
        key, values = items[0], items[1:]
        my_dict[key] = values


print my_dict

但是,我悲惨地失败了。文件非常大。请帮忙!

0 个答案:

没有答案
相关问题