Reputation: 653
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.
Upvotes: 1
Views: 40
Reputation: 22370
Consider utilizing a defaultdict and all:
from collections import defaultdict
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"]}
files = [file1, file2, file3, file4, file5]
new_files = defaultdict(list)
for f in files:
if all(first in f for first in list_str):
for first in list_str:
new_files[first].append(f[first])
new_files = dict(new_files)
print(new_files)
for file_name, objects in new_files.items():
with open(f'{file_name}.txt', 'w') as file_handle:
for thing_color, thing in objects:
file_handle.write(f'{thing_color} {thing}\n')
Output:
{'Ana': [['red', 'lamp'], ['big', 'car'], ['black', 'pen']], 'Beatrice': [['blue', 'notebook'], ['ugly', 'bike'], ['white', 'glue']], 'Mike': [['green', 't-shirt'], ['plastic', 'boat'], ['blue', 'pencil']]}
Ana.txt
:
red lamp
big car
black pen
Upvotes: 1
Reputation: 1732
I've kept the Files structure as you wanted
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"]}
new_files = {s:[] for s in list_str}
for i in range(1, 6):
f = eval(f'File{i}')
for k in list_str:
if k in f:
new_files[k].append(f[k])
# new_files == {'Ana': [['red', 'lamp'], ['big', 'car'], ['new', 'phone'], ['black', 'pen']], 'Beatrice': [['blue', 'notebook'], ['ugly', 'bike'], ['fried', 'egg'], ['black', 'computer'], ['white', 'glue']], 'Mike': [['green', 't-shirt'], ['plastic', 'boat'], ['toasted', 'bread'], ['blue', 'pencil']]}
## and know we should store them in files
for k, v in new_files.items():
with open(f'{k}.txt', 'a') as f:
f.write('\n'.join(map(lambda x: '> ' + ' '.join(x), v)))
one of the files output:
Upvotes: 1
Reputation: 1284
You'll need your list of files to be iterable somehow (I've put them in the list files
), but I'm getting your expected output with the following:
names = ["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"]}
files = [File1, File2, File3, File4, File5]
new_files = {name: [] for name in names}
for name in names:
for file in files:
if all(name in file.keys() for name in names):
new_files[name].append(file[name])
print(new_files)
Upvotes: 1