I have this code that prints matches found in two files using regex:
with open('ipCountry_list.txt','r') as csvfile1, open('city.txt', 'r', encoding="utf8" ) as file1:
with open('unmatch.txt', 'a+') as file2:
readCS = csv.reader(csvfile1, delimiter=';')
reader = file1.readlines()
for row in readCS:
for line in reader:
if all(re.findall(fr"\b{word}\b[^ ]", line, re.IGNORECASE) for word in row[:2]):
print(str(row) + line)
file2.write(str(row) + line)
break
if all(re.findall(fr"\b{word}\b[^ ]", line, re.IGNORECASE) for word in row[:3]):
print(str(row) + line)
file2.write(str(row) + line)
break
Output:
['TH', 'BANGKOK']'TH~10~Bangkok'
['ES', 'VALENCIA']'ES~VC~Valencia'
['US', 'AZ', 'PHOENIX']'US~AZ~Aguila'
['JP', 'KASHIWA']'JP~12~Kashiwa'
[CZ;PRAGUE 4 - aaa]
If you see ['US', 'AZ', 'PHOENIX']'US~AZ~Aguila' does not match.
[CZ;PRAGUE 4 - aaa] should be matching with 'CZ~10~Prague' but it can't get a match.
I am not sure what I am doing wrong, maybe my regex?
EDIT:
ipCountry_list.txt:
TH;BANGKOK;aaa
ES;VALENCIA;aaa
US;AZ;PHOENIX;aaa
JP;KASHIWA;aaa
CZ;PRAGUE 4 - aaa;
ZA;EAST LONDON;aaa
GB;BRIDGWATER;aaa
RU;MOSCOW;aaa
GH;TEMA;aaa
city.txt:
'TH~10~Bangkok'
'ES~VC~Valencia'
'US~AZ~Aguila'
'US~AZ~Phoenix'
'JP~12~Kashiwa'
'GB~ENG~Bridgwater'
'ZA~EC~East London'
'RU~MOW~Moscow'
'GH~AA~Tema'
'CZ~10~Prague'