import json
with open('/home/sentry/Documents/mark.txt', 'r') as f1,open('/home/sentry/Downloads/axtscz-english-first-names/Male.json', 'r') as f2:
content = f1.read()
data = json.load(f2)
for p_id in data:
name = p_id.get('name')
if name in content: # This is where my problem is.
print('True')
else:
print(content)
print (name)
The console will output both the text file each name in json file though all i want is a yes no answer.
mark.txt contains MY NAME IS MARK THANK YOU.
sorry about the capitals but the json files names are all capitals.
I have tried googling opening two files at once, but being a newbie im stumped why its not working.
2
Answers
import json
with open('/home/sentry/Documents/mark.txt', 'r') as f1,open('/home/sentry/Downloads/axtscz-english-first-names/Male.json', 'r') as f2: content = f1.read() data = json.load(f2)
for p_id in data: name = p_id.get('name') if name in content: print('Name exist') print (name)
Given you current format of
data
I would reshape it from a list of json into either aset()
or adict()
.Given:
A
set()
would look like:the
dict()
version might look like:In either case from there you can use the lookup to answer if a name in in it:
Resulting in:
Use the
set()
version if you don’t care about the rest of the data related to that name or thedict()
version if you do. Note that duplicate names will result in some data loss, but if you just want a Yes/No then this should get you going.