Q:

print similar word matches to the number using python

I have a text file which contains -
A 1 2 @ ~ 7 8 3 12389390  
B 1 3 6 ~ 1 1 9 -3 2 34789021 
C 4 5 ~ 2 4 1 1 $ -1 12389390
D 2 4 1 ~ 1 1 @ -1 6 @ 34789021
E 3 3 9 ~ 2 4 0 -1 2 12389390 34789021 25909052

The output should be -
A C E
B D E
E

Because the value of A,C,E have same numbers.
I have tried this code -
import re
maxline = 7
with open ('letter.txt','r') as file:
    for line in file:
        if re.match('^[a-zA-Z]+', line) is not None:
            print(line.split()[0])
                with open('letter.csv','a+') as output:
                output.write(line) 

The outputs are like -
A
B
C
D
E

I just print the first letter of each line. But could not find any logic to do just like the output should be.Is there any way to do this?

A:

Here is what you want to do : 
you need to find all the key sequences in your string i.e "12389390" like keys and i assume its length = 8. 
Find All the unique keys and loop over you file for check which line contains the current key, then get first element of that line. You are done.
import re    
numberToMatch = []
saveValues = {}
with open("D:\\untitled\\data.txt") as dataF :

            lines = dataF.readlines()
            for line in lines :
                print(line)
                numMatch = re.findall(r"[0-9]{8}(?![0-9])", line.rstrip())
                print(numMatch)
                for num in numMatch :
                    saveValues[num] = []

            for key_ in saveValues.keys() :
                        for line in lines:
                                if key_ in line :
                                    saveValues[key_].append(line.rstrip().split(" ")[0])

for k,v in saveValues.items():
    print (" ".join(x for x in v))

Output
A C E
B D E
E

