我如何找出有多少关键字从文件也在另一个文件?我有一个包含单词列表的文件,我试图弄清楚这些单词是否在另一个文件中。如何读取文件,并计算出一个特定的值
我有一个包含关键词的文件(keywords.txt),和我试图找出是否另一个文件包含(tweets.txt),其中包含的句子,包含任何关键字
def main() :
done = False
while not done:
try:
keywords = input("Enter the filename titled keywords: ")
with open(keywords, "r") as words:
done = True
except IOError:
print("Error: file not found.")
total = 0
try:
tweets = input("Enter the file Name titled tweets: ")
with open(tweets, 'r') as tweets:
except IOError:
print("Error: file not found.")
def sentiment_of_msg(msg_words_counter):
summary = 0
for line in tweets:
if happy_dict in line:
summary += 10 * **The number of keywords in the sentence of the file**
elif veryUnhappy_dict in line:
summary += 1 * quantity
elif neutral_dict in line:
summary += 5 * quantity
return summary
第一读取文本存储大文件。现在你打开文件,但后来你对这些文件什么都不做。后来你会做计算。 – furas
没有人愿意为你做功课,原因很多。问一个具体的问题来解决你的问题的一部分。现在你甚至没有接近。用开放(tweets,'r')作为推文后会发生什么:'? –
@AlexHall如果你不打算提出任何建议或提供帮助,Id感谢,如果你没有评论。谢谢! – HelloWorld4382