0

I hope you're well :) I'm beginner with Python. I hope my message will be clear

I'll try to summarize. I've some blog posts with a title (which a name of a recipe, for instance Salade Nicoise) and a picture only. I'd like to match the title of my blog post with the recipe of my json (which full of recipes).

Here is a code to display all the json:

import pandas as pd
import json
import re

class Exemple:
    def __init__(self, titre, image, url, temps, tempsprep, tempscui, autor, autorlink, typerecipe, recipe, peoplequantity, ingredients, reputation, difficulte, budget):
        self.titre = titre 
        self.image = image
        self.url = url
        self.temps = temps 
        self.tempsprep = tempsprep
        self.tempscui = tempscui
        self.autor = autor
        self.autorlink = autorlink
        self.typerecipe = typerecipe
        self.recipe = recipe
        self.peoplequantity = peoplequantity
        self.ingredients = ingredients
        self.reputation = reputation
        self.difficulte = difficulte
        self.budget = budget
        
    @classmethod 
    def from_json(cls, json_string):
        json_dict = json.loads(json_string)
        return cls(**json_dict)
        
    def __repr__(self):
        return f'{ self.titre }, { self.image }, { self.url }, { self.temps }, { self.tempsprep }, { self.tempscui }, { self.autor }, { self.autorlink }, { self.typerecipe }, { self.recipe }, { self.peoplequantity }, { self.ingredients }, { self.reputation }, { self.difficulte }, { self.budget }, '


with open('exemple.json', 'r') as json_file:
    list_data = json.loads(json_file.read())
    for l in list_data:
        check_list.append(Exemple(**l))
        
print(check_list)

For instance: the name of my post is Salade Nicoise. I'd like to search in my json the 'titre'which is the closest to Salade Nicoise (sometimes: La Salade Niçoise la vraie or La Salade Nicoise, etc) and display the infos of the json on my post. Do you have any ideas?

I've tried to do it with pandas but if it's not exactly the same title that's does not match.

df = pd.read_json('exemple.json')

dtitre = df.loc[df['titre'] == 'Mousse de foies de volaille']


print (dtitre[['titre', 'ingredients', 'reputation']]) 

I'll be happy if you can help me guys,

Louis
  • 360
  • 2
  • 12
  • Hi. There are actually two things to consider: will you always get at least one match? Will there potentially multiple matches? Anyway, this might help: https://stackoverflow.com/questions/17388213/find-the-similarity-metric-between-two-strings#17388505 – AlexNe Jul 03 '20 at 21:06
  • And some advice for future questions: When asking a question, try to abstract your problem as much as possible: You blockade is not how to use django, but given an arbitrary string, find it’s closest match in in a set. – AlexNe Jul 03 '20 at 21:10
  • Thanks for your answer I'll summarize my question – Louis Jul 03 '20 at 21:25
  • I want only one match :) – Louis Jul 03 '20 at 21:25
  • Summarizing also helps you understand your problem. I can’t tell you how often I started creating a question on Stackoverflow and discovered the solution in the process :) – AlexNe Jul 03 '20 at 21:38

0 Answers0