我有一个keyword_file
和一个log_file
,我想比较这两个文件并突出显示匹配文本的整行。你知道吗
keywords_file = open('keyword_file')
keywords_dict = {word: 0 for word in keywords_file.readlines().strip().split(' ')} # Iterate through all the words removing '\characters and generate a dict
# Then read the chat log
chat_log_file = open('log_file')
chat_log_words_generator = (word for word in chat_log_file.readlines().strip().split(' ')) # Create a generator with the words from the chat log
for word in chat_log_words_generator:
try:
word_count = keywords_dict[word]
except Key-error:
continue # The word is not a keyword
word_count += 1 # increment the total
keywords_dict[word] = word_count
我从网上得到了上面的代码,但是失败了。我收到的错误信息是:
is...
Trace back (most recent call last):
File "main.py", line 2, in <module>
keywords_file = open('keyword_file')
FileNotFoundError: [Err-no 2] No such file or directory: 'keyword_file
比较日志文件和关键字文件,并突出显示控制台上匹配的文本
代码:
相关问题 更多 >
编程相关推荐