Home > Software engineering >  Getting a file with lines and turning them into a dictionary with lists
Getting a file with lines and turning them into a dictionary with lists

Time:12-02

my problem is that I want to read a file with multiple lines e.g.:

be:was, were
I:you
Tom

Now I want to turn the first element (before the ':') into the key of the dict and everything what comes after the ':' into a list. Every list entry is separated with ','

So it should look like this:

words_dict = {"be" : ["was", "were"], "I" : ["you"], "Tom" : []}

I came up with something like this:

with open(words, 'r') as file:
    for line in file:
        words = line.strip().split(':')

I don't know if this is the right approach and what to do next.

CodePudding user response:

perhaps something along the lines of

with open(words, 'r') as file:
    for line in file.readlines(1024):
        split = line.removesuffix('\n').split(':', maxsplit=1)

        if len(split) == 2:
            vals = [val.strip() for val in split[1].split(',')]
        else: # : not found in line
            vals = []

        d[split[0]] = vals

If you can clean up the data to always guarantee there's a : after the key, the loop can be simplified greatly;

with open(words, 'r') as file:
    for line in file.readlines(1024):
        key, value = line.removesuffix('\n').split(':', maxsplit=1)
        d[key] = [val.strip() for val in value.split(',')]

CodePudding user response:

You should first split the line with ':' to get the keys and the rest of the string (if it has) which are going to be values later. Then split the values with ',' and filter them if they are not empty after stripping.

Try this one:

d = {}
with open(words) as f:
    for line in f:
        key, *values = line.strip().split(':', maxsplit=1)
        values = ''.join(values)
        values = [w for word in values.split(',') if (w := word.strip())]
        d[key] = values
print(d)

output:

{'be': ['was', 'were'], 'I': ['you'], 'Tom': []}

Note that this works because: first, "join" method will not raise exception for empty iterables, second, "split" also doesn't complain with empty strings.

CodePudding user response:

A short solution:

data = {}
with open('words.txt', 'r') as file:
    for line in file.readlines():
        key, *value = line.strip().split(':')
        data[key] = list(filter(','.join(value).split(',')))
print(data)

Output:

{'be': ['was', ' were'], 'I': ['you'], 'Tom': []}

CodePudding user response:

You should be able to use a dictionary comprehension:

with open(words, 'r') as file:
    words = {(l:=s.rstrip('\n').split(':'))[0]:l[1].split(', ') if len(l)>1 else []
             for s in file}
  • Related