gpt4 book ai didi

python - 如何在两个文本文件中查找单词

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:23 25 4
gpt4 key购买 nike

脚本的第一部分是正确的(它删除了 http://www.)。稍后我需要检查 source 中的单词是否存在于 exists 中。

source = open('/net/sign/temp/python_tmp/script1/source.txt','r')
exists = open('/net/sign/temp/python_tmp/script1/exists.txt','r')

with source as f:
lines = f.read()
lines = lines.replace('http://','')
lines = lines.replace('www.','')

for a in open('/net/sign/temp/python_tmp/script1/exists.txt'):
if a == lines:
print("ok")

source.txt内容:

www.yahoo.it
www.yahoo.com
www.google.com
http://www.libero.it

exists.txt的内容:

www.yahoo.com

最佳答案

像这样的东西应该可以工作:

source_words = set()
with open('source.txt') as source:
for word in source.readlines():
source_words.add(word.replace('http://','').replace('www.','').strip())

exist_words = set()
with open('exist.txt') as exist:
for word in exist.readlines():
exist_words.add(word.replace('http://','').replace('www.','').strip())

print("There {} words from 'source.txt' in 'exists.txt'".format(
"are" if exist_words.intersection(source_words) else "aren't"
))

如果您需要获取两个文件中都存在的确切单词,它们在交集结果中:

print("These words are in both files:")
for word in exist_words.intersection(source_words):
print(word)

关于python - 如何在两个文本文件中查找单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34590958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com