简体   繁体   中英

Getting a file with lines and turning them into a dictionary with lists

my problem is that I want to read a file with multiple lines eg:

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.

perhaps something along the lines of

with open(words, 'r') as file:
    for line in file.readlines(1024):
        split = line.rstrip('\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.rstrip('\n').split(':', maxsplit=1)
        d[key] = [val.strip() for val in value.split(',')]

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.

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': []}

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM