python处理文件中有多个json对象,python在一个文件中提取多个Json对象

I am very new to Json files. If I have a json file with multiple json objects such as following:

{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",

"Code":[{"event1":"A","result":"1"},…]}

{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",

"Code":[{"event1":"B","result":"1"},…]}

{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",

"Code":[{"event1":"B","result":"0"},…]}

I want to extract all "Timestamp" and "Usefulness" into a data frames:

Timestamp Usefulness

0 20140101 Yes

1 20140102 No

2 20140103 No

Does anyone know a general way to deal with such problems? Thanks!

解决方案

Use a json array, in the format:

[

{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",

"Code":[{"event1":"A","result":"1"},…]},

{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",

"Code":[{"event1":"B","result":"1"},…]},

{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",

"Code":[{"event1":"B","result":"0"},…]},

...

]

Then import it into your python code

import json

with open('file.json') as json_file:

data = json.load(json_file)

Now the content of data is an array with dictionaries representing each of the elements.

You can access it easily, i.e:

data[0]["ID"]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值