I have a lot of files with specific strings that must be grouped into separated files based on those strings.
I have one list of strings that must be present in the file and when all of them are present in the same file, they will extracted and included into one second file. My initial idea is to convert the text to dictionary and after, if all the elements in list are present in the dictionary, extract them and create, add, to the new file.
list_str = ["Ana", "Beatrice", "Mike"]
File1= {"Ana":["red", "lamp"], "Beatrice":["blue", "notebook"], "Mike":["green", "t-shirt"]}
File2= {"Ana":["big", "car"], "Beatrice":["ugly","bike"], "Mike":["plastic", "boat"]}
File3= {"Beatrice":["fried","egg"], "Mike":["toasted","bread"]}
File4= {"Ana":["new","phone"], "Beatrice":["black","computer"]}
File5= {"Ana":["black","pen"], "Beatrice":["white","glue"], "Mike":["blue","pencil"]}
With this info, my expected result is:
new_files = {"Ana": [ ["red", "lamp"],["big", "car"],["black","pen"]], "Beatrice": [["blue", "notebook"], ["ugly","bike"], ["white","glue"] ], "Mike":[["green", "t-shirt"],["plastic", "boat"], ["blue","pencil"] ] }
And after I will convert the dictionary new_files using their keys as file names and their contents in each list as a line.
Like this in the same order that File1, File2, ..., FileN are processed
Ana.txt
>red lamp
>big car
>black pen
Any suggestion? Meanwhile, I will keep trying.
