简体   繁体   中英

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:

{"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. 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.

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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