【发布时间】:2022-01-06 22:09:29
【问题描述】:
我之前发布了一个关于在浏览器历史记录中读取字符串元素的问题,但现在我意识到我需要将该字符串文件的元素与外部 .txt 文件进行比较。这是我使用的。
from browser_history.browsers import Chrome
f = Chrome()
outputs = f.fetch_history()
string1 = str(outputs.histories)
file1 = open("grey.txt", "r")
readfile = file1.read()
if string1 in readfile:
print("TRUE")
else:
print("NAH FAM")
file1.close()
我在 .txt 文件中有 google 一词。我知道 google.com 在浏览器历史模块输出的字符串中。
if 'google' in str(outputs.histories):
print(True)
那行代码打印正确,但是使用外部文本文件似乎不起作用。
【问题讨论】:
-
文件末尾是否有尾随换行符?如果你在 file1.read() 之后添加“.strip()”会改变什么吗?
-
@inteoryx。我想进一步的问题是文本文件里面会有几个术语,如果字符串中只有一个术语与文本文件中的几个术语之一匹配,它将吐出一个真实的值。现在,除非只有一个值(该值为真),否则它将吐出 false。
标签: python string text browser-history