【问题标题】:python comparing text file - if else printingpython比较文本文件 - 如果其他打印
【发布时间】:2018-08-09 21:51:04
【问题描述】:

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

keycountry.txt

UK USA Germany

国家/地区.txt

Brexit - UK
USA UK Relations
France win world cup

keylink.txt

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

链接.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") 在没有匹配项时不会显示。我尝试将打印语句放在循环内部和外部,但我无法解决问题。

如果匹配,输出应该是这样的:

LINKS
www.abc.com
www.eee.com
matching

COUNTRY
Brexit-UK
USA UK Relations
matching

如果不匹配,输出应该是这样的:

LINKS
Not matching
COUNTRY
Not matching

如何解决这个问题?

【问题讨论】:

  • 最好的方法是调试你的脚本,有一个名为pdb的内置工具,或者在一些fance IDEs中,调试器也是内置的,使用起来简单有趣.

标签: python for-loop if-statement


【解决方案1】:

似乎您的问题一方面与 for-else 构造有关。否则将始终在您的代码中执行。

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

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

【讨论】:

  • 非常感谢! @io_exception 这正是我想要的。
【解决方案2】:

您可以将结果保存到列表中,并在找到所有匹配项后打印结果。

import re

keycountryfile = '''UK USA Germany'''
countryfile = '''Brexit - UK
USA UK Relations
France win world cup'''

links = '''www.abc.com
www.eee.com'''

links_data = links.split()

keys = '''www.abc.com
www.ddd.com
www.eee.com'''
keys_data = keys.split()


keys_split = keys_data

matching_links = []
not_links = []
for url in keys_split:
    if url in links_data:
        matching_links.append(url)
    else:
        not_links.append(url)

keys = set(keycountryfile.split())

matching_country = []
not_country = []
for line in countryfile.split():
    words = set(word.lower() for word in re.findall(r'\w+', line))
    if keys & words:
        matching_country.append(line)
    else:
        not_country.append(line)

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

print("COUNTRY")
print()
if matching_country:
    print('\n'.join(matching_country))
    print("matching")

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

print("COUNTRY")
if not_country:
    print('\n'.join(not_country))
print("Not matching")

你可以试试这个代码here

【讨论】:

  • 这打印匹配和不匹配。我试图一次只打印其中一个。如果项目匹配,则打印匹配的链接和国家,如果没有匹配,则显示“不匹配”。同样在您的 matching_country 代码中,它不显示匹配的国家/地区。
猜你喜欢
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多