getting the value from text file after the colon or before the colon in python

2024/7/8 7:56:15

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

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
https://en.xdnf.cn/q/120575.html

Related Q&A

How to remove the background of an object using OpenCV (Python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to move zeros to the end of a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

My program only appends one user input to list when main is looped

This is part of my code for a guessing game. I want to count the guesses of a player, and then append their name and number of guesses to a list that is later written or appended to file. As of now, it…

How to split a python dictionary for its values on matching a key

my_dict1 = {a:1, chk:{b:2, c:3}, e:{chk:{f:5, g:6}} }I would like to loop through the dict recursively and if the key is chk, split it. Expected output:{a:1, b:2, e:{f:5}} {a:1, c:3, e:{f:5}} {a:1, b:2…

PyException: ImportError: No module named domreg

I am getting the below error while running this script ("from xml.dom import minidom") from chaquopy androi application python console. PyException: ImportError: No module named domreg

Plotting polynomial with given coefficients

Im trying to plot a polynomial with coefficients given in array:input: [an,a(n-1),...,a0] output: plot of polynomial anx^n + a(n-1)x^(n-1) + ... + a0I would like to use matplotlib polt() function so I…

grouping data using unique combinations

n my below data set, I need to find unique sequences and assign them a serial no ..DataSet :user age maritalstatus product A Young married 111 B young married 222 C young Single 111 D…

Python - Sorting in ascending order in a txt file

I had a huge document that I parsed using regex to give a txt file (json.dump) similar to the following:{"stuff": [{"name": ["frfer", "niddsi", ], "number&q…

How to make setuptools install a wheel containing multiple packages?

Suppose this wheel:M Filemode Length Date Time File - ---------- -------- ----------- -------- --------------------------------------------rw-rw-r-- 1358 26-Sep-2018 21:08…

Python - Randomly select words to display in a quiz [duplicate]

This question already has answers here:How can I randomly select (choose) an item from a list (get a random element)?(18 answers)Closed 9 years ago.Im in need of some help. Here we have my two lists:w…