Is there any better way to do this?
import json
product_data = [x for x in open('product_strings.txt', 'r', encoding='utf-8').read().split(',')]
json_data = json.load(open('tedata.json', 'r', encoding='utf-8'))
for product_no in json_data:
for product in product_data:
if product.lower() in json_data[product_no]['name'].lower():
print (product.lower(), json_data[product_no]['name'])
elif product.lower() in json_data[product_no]['des'].lower():
print (product.lower(), json_data[product_no]['name'])
tedata.json contains (6mb with ~1000 objects)
{
"12345": {
"owner": "BMW int",
"doman": "20/01/2016",
"des": "a beautiful BMW with improvements with respect to older version of bmw.",
"dopur": "25/07/2016",
"purchaser": "Mitsubishi, Japan",
"name": "BMW-8"
},
"12346": {
"owner": "audi",
"doman": "20/01/2016",
"des": "a beautiful skoda with improvements with respect to older version of skoda.",
"dopur": "25/07/2016",
"purchaser": "Mokoto, Japan",
"name": "skoda-1"
}
}
product_strings file contains (small with ~100 such string)
audi,bmw,jaguar
code for testing:
import json
tedata = """{
"12345": {
"owner": "BMW int",
"doman": "20/01/2016",
"des": "a beautiful BMW with improvements with respect to older version of bmw.",
"dopur": "25/07/2016",
"purchaser": "Mitsubishi, Japan",
"name": "BMW-8"
},
"12346": {
"owner": "audi",
"doman": "20/01/2016",
"des": "a beautiful skoda with improvements with respect to older version of skoda.",
"dopur": "25/07/2016",
"purchaser": "Mokoto, Japan",
"name": "skoda-1"
}
}"""
product_strings = "audi,bmw,jaguar"
product_data = [x for x in product_strings.split(',')]
json_data = json.loads(tedata)
for product_no in json_data:
for product in product_data:
if product.lower() in json_data[product_no]['name'].lower():
print (product.lower(), json_data[product_no]['name'])
elif product.lower() in json_data[product_no]['des'].lower():
print (product.lower(), json_data[product_no]['name'])
##prints ===>bmw BMW-8
ttl
andabst
? Is this code even having the desired functionality with those results of theif
/elif
? – I'll add comments tomorrow yesterday