0

I have two main dictionaries:

dict_main1 = {}
dict_main2 = {}

And then I open many dictionaries (below only 6 of 26 I have) which store the values of the main dictionaries depending on one particular string:

string1 = {}
string2 = {}
string3 = {}
string4 = {}
string5 = {}
string6 = {}

for key, value in dict_main1.items():
    if 'string1' in key:
        string1[key] = dict_main1[key]
    elif 'string2' in key:
        string2[key] = dict_main1[key]
    elif 'string3' in key:
        string3[key] = dict_main1[key]
    ......

for key, value in dict_main2.items():
    if 'string4' in key:
        string4[key] = dict_main2[key]
    elif 'string5' in key:
        string5[key] = dict_main2[key]
    elif 'string6' in key:
        string6[key] = dict_main2[key]
    ......

How can I open a file for each strin#={} in a pythonic way?. I would like to avoid doing it one by one (as in the example below):

FI = open ('string1', w)
    for key, value in string1.items():
        OUT = key + '\n' + value + '\n'
        FI.write(OUT)
gusa10
  • 169
  • 1
  • 1
  • 10
  • This is confusing. What do your `......`'s represent? – Mateen Ulhaq Sep 30 '18 at 06:58
  • you may want to use `pickle` module for that. – angrysumit Sep 30 '18 at 07:00
  • what are you trying to achieve? maybe be there is better solution for it – SocketPlayer Sep 30 '18 at 07:01
  • It represents I have many other dictionaries to open (I stated I put as an example only 6 of the 23 - see I wrote the script as example only till 'string6' – gusa10 Sep 30 '18 at 07:01
  • Have a look at YAML - it's designed to provide a seamless bridge between text-formatted lists of keywords and arguments and Python dictionaries: https://stackoverflow.com/questions/1773805/how-can-i-parse-a-yaml-file-in-python – Artur Sep 30 '18 at 07:02
  • What happens is that I have 13 different words that each of my main two dictionaries contains within its key. I need to create 26 different files, one per different word – gusa10 Sep 30 '18 at 07:04
  • the real question is: why are you saving them? if it only for display, so maybe separate files is ok, but maybe one file with sections would be better. and if it for later use by code, maybe json/pickle/yaml/shelve would be better – SocketPlayer Sep 30 '18 at 07:25

2 Answers2

2

First of all you don't need 99999 dicts, just use one with dicts inside it.

for example:

from collections import collections.defaultdict

my_keys = ['str1', 'str2', ....]
container_dict = defaultdict(dict)

for key, value in dict_main.items():
    for k in my_keys:
        if k in key:
            container_dict[k][key] = value

now for the files, just use for:

for string, strings_dict in container_dict:
    with open(string, "wb") as f:
        # format string dict... and save it

i didn't run this code, so maybe there are some bugs, but i suppose it ok

SocketPlayer
  • 166
  • 6
1

It might be useful to use a single dictionary data structure rather than maintaining 26 different dictionaries.

def split_dict(d, corpus):
    dicts = {{} for w in corpus}
    for k, v in d.items():
        word = next(filter(lambda w: w in k, corpus))
        dicts[word][k] = v
    return dicts

dict_main = {...}
corpus = [f'string{i}' for i in range(1, 4)]
dict_split = split_dict(dict_main, corpus)

Now, just loop through dict_split:

for word, d in dict_split:
    with open(word, 'w') as f:
        for key, value in d.items():
            f.write(f'{key}\n{value}\n')
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135