2

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?

2 Answers 2

2

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']
Sign up to request clarification or add additional context in comments.

3 Comments

(the edit was just to make it more obvious what the initial part was doing all along, fwiw -- glad you've got what you need now :))
Yep, I understand now. Thanks for making that more obvious for me :)
this is the right answer...
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.