xn139
xn139

Reputation: 395

How would I get the json key names by doing a string split here? (Python)

I have a json var: [u'{"Average_Rating":3.75,"Number_Of_Users_Who_Have_Watched_This_Movie":90,"title":"Quiz Show (1994)"}']

And I want to get the '3.75', '90' and 'Quiz Show (1994)'. I've tried doing:

  for i in var:
      i.split(":")[1:-3]

but it's not working. I'm sure this is something simple but just don't get how to use it properly! Could anyone please help?

EDIT:

Normally, I would just use the json library but I'm using Apache PySpark and there are a few issues around JSON - how can I do it using just string splitting?

Upvotes: 0

Views: 57

Answers (1)

Neil
Neil

Reputation: 14323

You are trying to get information from a JSON string, you should really use json for this.

var = [u'{"Average_Rating":3.75,"Number_Of_Users_Who_Have_Watched_This_Movie":90,"title":"Quiz Show (1994)"}']
import json

for i in var:
    print json.loads(i)["Average_Rating"]

Per request of the original poster:

var = [u'{"Average_Rating":3.75,"Number_Of_Users_Who_Have_Watched_This_Movie":90,"title":"Quiz Show (1994)"}']

for i in var:
    print(i.split(":")[1].split(",")[0])

Upvotes: 1

Related Questions