I am using python to parse a txt file. My text file looks like this:

Text below here:
Master : 000000 Slave : 000001 000002
Master : 000003 Slave : 000004 000005
Master : 000006 Slave : 000007 000008
Master : 000010 Slave :
Master : 000012 Slave :
Master : 000014 Slave :
Master : 000016 Slave : 000017 000018
Master : 000020 Slave : 000021 000022
Master : 000024 Slave : 000025 000026
Master : 000028 Slave : 000029 000030
Master : 000032 Slave : 000033 000034
Master : 000036 Slave : 000037 000038
I want all these values inside a dictionary with master as keys and slaves as values.
{master1: [slave1, slave2], master2: [slave1, slave2]}
KEYS VALUES
00000 00001 , 00002
00003 00004 , 00005...
I am a python newbie. Please help. Thank you
You can use
remodule and regular expressions to find the master and slave values with a pattern, then simply get the groups from the pattern and add them to the dictionary accordingly:Output:
Useful:
remodule (built-in)