python比较文本文件-如果不打印 [英] python comparing text file - if else printing

查看:71
本文介绍了python比较文本文件-如果不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个以下格式的文本文件

I have 4 text files in the following formats

keycountry.txt

keycountry.txt

UK USA Germany

country.txt

country.txt

Brexit - UK
USA UK Relations
France win world cup

keylink.txt

keylink.txt

www.abc.com
www.ddd.com
www.eee.com

link.txt

www.abc.com
www.eee.com

代码:

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")

在代码中print("matching")重复多次.我知道,因为它在循环内,所以会重复,当没有匹配项时,不会显示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

如何处理?

推荐答案

似乎您的问题一方面与for-else构造有关.否则,其他将始终以您的代码执行.

Seems like your issue is on one hand related with the for-else construct. Else will always get executed in your code.

此外,以kaihami的答案为基础,要实现您所描述的内容,您需要将匹配的链接/行存储在单独的结构(如列表)中,然后检查该列表是否为空以打印匹配的条目或字符串"Not"匹配",这是我建议的解决方案:

Moreover, building upon kaihami's answer, to achieve what you are describing you need to store the matching links/lines in a separate structure like a list and then check if that list is empty to print the matched entries or the string "Not matching", here is my proposed solution:

import re

keycountryfile = "keycountry.txt"
countryfile = "country.txt"

with open('links.txt', 'r') as links:
    links_data = [line.strip() for line in links.readlines()]

with open('keylink.txt', 'r') as keys:
    keys_links = set([line.strip() for line in keys.readlines()])


matching_links = []
for url in links_data:
    if url in keys_links:
        matching_links.append(url)

print('LINKS')
if matching_links:
    print('\n'.join(matching_links))
    print("matching")
else:
    print("Not matching") 

print()

with open(keycountryfile , "r") as f:
    country_keys = set(key.lower() for key in 
                       re.findall(r'\w+', f.readline()))

matching_lines = []
with open(countryfile) as f:
    for line in f:
        words = set(word.lower() for word in re.findall(r'\w+', line))
        if country_keys & words:
            matching_lines.append(line.strip())
    print("COUNTRY")
    if matching_lines:
        print('\n'.join(matching_lines))
        print("matching")
    else:            
        print("Not matching")

这篇关于python比较文本文件-如果不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆