简体   繁体   中英

Comparing a string to a .txt file

I have posted a question earlier about reading a string element in browser history, but now I realized I need to compare the elements of that string file to an external.txt file. Here is what I've done using.

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()

I have the word google in the.txt file. I know google.com is in the string outputted by the browser-history module.

if 'google' in str(outputs.histories):
    print(True)

That line of code prints true, however using an external text file does not seem to work.

I agree with @inteoryx in the comment.

I think the problem is with the whitespaces. When you load a external file as string, the whole line is selected.

Moreover, I think there is an error in this too, `

if string1 in readfile :

it should be done like this:

if readfile in string1:

You should do something like this:

from browser_history.browsers import Chrome

f = Chrome()

outputs = f.fetch_history()

string1 = str(outputs.histories)

file1 = open("grey.txt", "r")

readfile = file1.read()

readfile = str(readfile)
readfile = readfile.strip()

if readfile in string1:
    print("TRUE")
else:
    print("NAH FAM")

file1.close()

EDIT: for multiple checks it can be done like this:

from browser_history.browsers import Chrome

f = Chrome()

outputs = f.fetch_history()

string1 = str(outputs.histories)
count = 0

file1 = open("grey.txt", "r")

readfile = file1.read()

readfile = str(readfile)
readfile = readfile.strip()

read = readfile.split(',')

for i in read :
    
    if i in string1:
        count = count+1


if count == len(read) :
    print("True" , len(read))
else :
    print("NAH FAM")
file1.close()

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM