【发布时间】:2016-01-04 23:18:09
【问题描述】:
脚本的第一部分没问题(它删除了http:// 和www.)。稍后我需要检查 source 中的单词是否存在于存在。
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
【问题讨论】:
-
完全不清楚您要做什么。您是否需要查找两个文件中存在的所有单词?你对一个词的定义是什么?区分大小写怎么办?另外我不认为
read正在做你期望它做的事情,否则你不会调用返回值lines。 -
为什么你
openexists.txt文件两次? -
首先,您必须将单词提取到一些数据结构中(我相信集合会很完美)。目前您只操作线条。然后你必须比较这些集合是否相交。你清楚怎么做吗?
-
您能否提供一些
source.txt和exists.txt文件的示例内容? -
if ("http://" and "www.") in a:?