日志文件为JSON格式

时间:2017-01-16 22:56:30

标签: python json

我收到了需要解析的日志文件。它在JSON中,但它是一个日志:

{
  "device_uuid": "abc",
  "os_ver": "9.3.1",
  "device_new": true,
  "carrier": "Comcast Cable",
  "model": "iPad Air",
  "customer_ids": {
    "customer_id": "abc123"
  }
}
{
  "device_uuid": "cde",
  "os_ver": "10.2",
  "device_new": true,
  "carrier": "Frontier Communications",
  "model": "iPhone 7",
  "customer_ids": {
    "customer_id": "cde123"
  }
}
{
  "device_uuid": "fgh",
  "os_ver": "10.2",
  "device_new": true,
  "carrier": "ATT",
  "model": "iPhone 6",
  "customer_ids": {
    "customer_id": "fgh123"
  }
}

我的目标是将上面的日志文件解析为pandas数据帧。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

鉴于以下示例数据,可以使用以下内容导入DataFrame

log_lines = pd.concat([pd.read_json(line) for line in my_json_lines])

注意:执行此导入需要plenty of other ways

my_json_lines = (
    '{"device_uuid":"abc", "os_ver":"9.3.1", "device_new":true,'
    '"carrier":"Comcast Cable","model":"iPad Air",'
    '"customer_ids":{"customer_id":"abc123"}}',
    '{"device_uuid":"cde","os_ver":"10.2","device_new":true,'
    '"carrier":"Frontier Communications","model":"iPhone 7",'
    '"customer_ids":{"customer_id":"cde123"}}',
    '{"device_uuid":"fgh","os_ver":"10.2","device_new":true,'
    '"carrier":"ATT","model":"iPhone 6",'
    '"customer_ids":{"customer_id":"fgh123"}}'
)