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?
No comments:
Post a Comment