简体   繁体   English

将多行JSON转换为python字典

[英]Convert multiline JSON to python dictionary

I currently have this data in a file which is multiple JSON rows (about 13k rows but the example below is shortened: 我目前在一个包含多个JSON行(大约13,000行)的文件中拥有此数据,但以下示例已缩短:

{"first_name":"John","last_name":"Smith","age":30}
{"first_name":"Tim","last_name":"Johnson","age":34}

I have the following code: 我有以下代码:

import json
import codecs

with open('brief.csv') as f:
    for line in f:
        tweet = codecs.open('brief.csv', encoding='utf8').read()
        data = json.loads(tweet)
print data
print data.keys()
print data.values()

If I only have one row of data in my file, this works great. 如果我的文件中只有一行数据,那么效果很好。 However, I can't seem to figure out how to go row by row to change each row into a dictionary. 但是,我似乎无法弄清楚如何逐行将每一行更改为字典。 When I try to run this on multiple lines, I get the ValueError(errmsg("Extra data", s end, len(s))) error due to the code only wanting to deal with two curly braces, IE the first row. 当我尝试在多行上运行此代码时,由于代码只想处理两个大括号,即第一行,所以我收到ValueError(errmsg(“额外数据”,s结束,len(s)))错误。 I ultimately want to be able to select certain keys (like first_name and age) and then print out only those values out of my file. 我最终希望能够选择某些键(例如first_name和age),然后仅从文件中打印出这些值。

Any idea how to accomplish this? 任何想法如何做到这一点?

You're reading the whole file once for each line... try something like this: 您正在为每一行读取整个文件一次,请尝试以下操作:

import json
import codecs

tweets = []

with codecs.open('brief.csv', encoding='utf8') as f:
    for line in f.readlines():
        tweets.append(json.loads(line))

print tweets

for tweet in tweets:
    print tweet.keys()
    print tweet['last_name']

May be you can try like below more simplify 也许你可以像下面这样简化

>>> import simplejson as json 
>>> with open("brief.csv") as f:
...     for line in f:
...         data = json.loads(line)
...         print data
...         print data.values()
...         print data.keys()

{'first_name': 'John', 'last_name': 'Smith', 'age': 30}
['John', 'Smith', 30]
['first_name', 'last_name', 'age']
{'first_name': 'Tim', 'last_name': 'Johnson', 'age': 34}
['Tim', 'Johnson', 34]
['first_name', 'last_name', 'age']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM