mangodreamz mangodreamz - 1 month ago 8
Python Question

Looping through file and trying to group values by keys

I have a text file with the following:

1 cdcdm
1 dhsajdhsa
2 ffdm
2 mdff
3 ccdfm
3 cdmfc
3 fmdcc


My goal is for the output to look like this:

1 : cdcdm, dhsajdhsa
2 : ffdm, mdff
3 : ccdfm, cdmfc, fmdcc


I wrote the following code, but for some reason, I'm not getting the expected output.

value_list = ''
cur_key = None
key = None
f = open('example.txt', 'r')
for line in f.readlines():
try:
key, value = line.split()
key = key.strip()
value = value.strip()
if cur_key == key:
value_list = value_list + "," + value
else:
if cur_key:
print(cur_key + ":" +value_list)
cur_key = key
value_list = ''
else:
cur_key = key
except Exception as e:
continue


I'm getting the following output:

1:,dhsajdhsa
2:,mdff


How can I modify my code to get this to work?

Thanks,

Mango

Answer

A minimally changed implementation might look like this

with open('example.txt', 'r') as f:
    cur_key = None
    value_list = []
    for line in f.readlines():

        key, value = line.split()
        value = value.strip()

        if not cur_key:
            cur_key = key

        if cur_key == key:       
            value_list.append(value)
        else:
            print(cur_key + ":" + ', '.join(value_list))
            cur_key = key
            value_list = [value]
    print(cur_key + ":" +', '.join(value_list))

output:

1:cdcdm, dhsajdhsa
2:ffdm, mdff
3:ccdfm, cdmfc, fmdcc

So we need to make sure cur_key has a value for the first iteration. So set it if not None. Also when we find a new key we shouldn't reset value_list to be blank. It should be the set to the value read on that line, so the lien is not skipped. Also to catch the final groups line we should print the values again outside the loop at the end.