Skip to content Skip to sidebar Skip to footer

Filtering A Text File With A Certain String In A Line Then Returning Another String Within The Line

this is my text file named team.json my problem is that I only know how to return a specific line, For example i select a filter location, like PQ how would i return the names that

Solution 1:

once you have parsed your json data you could use this to extract members in the PQ location:

member_in_pq = [member for member in my_data['members'] \
    if member['location'] == 'PQ']
print(member_in_pq)
# -> [{'age': 24, 'location': 'PQ', 'name': 'Jesse'}, ...]

or, if you want just the names:

member_in_pq = [member['name'] for member in my_data['members'] \
    if member['location'] == 'PQ']
print(member_in_pq)
# -> ['Jesse', 'Jez',..., 'Cherry']

i recommend using the parsed version of your data instead of going back and parsing the file (again) manually.


Post a Comment for "Filtering A Text File With A Certain String In A Line Then Returning Another String Within The Line"