I only need the last number 25 so i can change it to int(). I have this text file:
{"members": [{"name": "John", "location": "QC", "age": 25},{"name": "Jesse", "location": "PQ", "age": 24},
}
then return the value of the age
This what I have tried so far, so I am so stuck... What kind of function do I use to parse it? Is that what you call it? I'm new to programming and in python.
import json
import pprintmy_data = json.loads(open("team.json.txt").read())print 'members whose location is in PQ'
member_in_pq = [member for member in my_data['members'] \if member['location'] == 'PQ']
pprint.pprint(member_in_pq)
By the way I tried this code but it gives an error:
result = my_data.split(':')[-1]
I'm not sure what you're asking how to do, but will make an attempt to guess.
First of all, the text you show isn't valid JSON and would need to be changed to something like this:
{ "members": [ {"name": "John", "location": "QC", "age": 25}, {"name": "Jesse", "location": "PQ", "age": 24}]
}
Here's something that would print out all the data for each member in location PQ
:
import json
import pprintmy_data = json.loads(open("team.json.txt").read())members_in_pq = [member for member in my_data['members']if member['location'] == 'PQ']print 'members whose location is in PQ'
for member in members_in_pq:print ' name: {name}, location: {location}, age: {age}'.format(**member)
Output:
members whose location is in PQname: Jesse, location: PQ, age: 24