Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在两个文本文件中查找单词_Python_String - Fatal编程技术网

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

Python 如何在两个文本文件中查找单词,python,string,Python,String,脚本的第一部分是正常的(它删除了http://和www.)。稍后我需要检查源代码中的单词是否存在 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.rep

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

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
www.yahoo.com

的内容存在.txt

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

好的,从您的示例文件判断,您实际要做的是查找两个文本文件共享的行。如果您的文件不是巨大的,一个简单的解决方案是读入文件并计算它们的线集的交点

>>> with open('source.txt') as s, open('exists.txt') as e:
...     result = set(s).intersection(e)
... 
>>> result
set(['www.yahoo.com\n'])
之后,您可以将
'http://'
'www.
替换为

result = [x.replace('http://', '').replace('www.', '') for x in result]

如果你想的话。

类似的方法应该可以:

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)

根本不清楚你想做什么。您需要查找两个文件中存在的所有单词吗?你对一个词的定义是什么?那么区分大小写呢?另外,我不认为
read
是在做你期望它做的事情,否则你就不会调用返回值
lines
。为什么你
打开
exists.txt
文件两次?首先,你必须将单词提取到一些数据结构中(我相信集合会很完美)。目前,您仅操纵直线。然后,如果这些集合相交,则必须对它们进行比较。您是否清楚如何操作?能否为
source.txt
exists.txt
文件提供一些示例内容?
如果在a:
中(“http://”和“www.”),您不知道可以使用
打开多个文件。回答得好。:)@erip对于未知数量的文件来说,这只会变得有点棘手:)您无缘无故地创建了一个集合和两个列表