简体   繁体   中英

Read file line by line and print if it contains two different strings

So I have a file called 'clears' and I want to see if a line contains two different strings that are in a list and if so to print those lines I cant get it to work.

for pos in positions:
    for line in open('clears'):
        if pos[0] and pos[3] in line:
            print line

I also tried f

or pos in positions:
        for line in open('clears'):
            if pos[0] in line and pos[3] in line:
                print line

Thats what I tried but I get a TypeError: 'in <string>' requires string as left operand I can get it to print if there is only one condition but I am not sure how to do if there is two.

Thanks

I guess you mean this (EDIT, back to first version):

for pos in positions:
    for line in open('clears'):
        if pos[0] in line and pos[3] in line:
            print line

But also, the elements of pos must be strings.

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