I have 4 text files in the following formats
keycountry.txt
UK USA Germany
country.txt
Brexit - UK
USA UK Relations
France win world cup
keylink.txt
www.abc.com
www.ddd.com
www.eee.com
link.txt
www.abc.com
www.eee.com
The code:
import re
keycountryfile = "keycountry.txt"
countryfile = "country.txt"
links = open('links.txt', 'r')
links_data = links.read()
links.close()
keys = open('keylink.txt', 'r')
keys_data = keys.read()
keys.close()
keys_split = keys_data.splitlines()
print('LINKS')
for url in keys_split:
if url in links_data:
print(url)
print("matching")
else:
print("Not matching")
keys = set(key.lower() for key in
re.findall(r'\w+', open(keycountryfile , "r").readline()))
print("COUNTRY")
with open(countryfile) as f:
for line in f:
words = set(word.lower() for word in re.findall(r'\w+', line))
if keys & words:
print(line, end='')
print("matching")
else:
print("Not matching")
In the code print("matching") is repeating multiple times. I know since it's inside the loop it will repeat, print("Not matching") is not displaying when there are no matches. I tried putting the print statements inside and outside the loop but I just wasn't able to rectify the problem.
The output, if it matches, should be like:
LINKS
www.abc.com
www.eee.com
matching
COUNTRY
Brexit-UK
USA UK Relations
matching
The output, if it doesn't match, should be like:
LINKS
Not matching
COUNTRY
Not matching
How to go about this?
Aucun commentaire:
Enregistrer un commentaire