简体   繁体   English

逐行读取文件并打印包含两个不同字符串的文件

[英]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. 因此,我有一个名为“ clears”的文件,我想查看一行是否包含列表中的两个不同的字符串,如果要打印这些行,则无法正常工作。

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

I also tried f 我也试过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. 那就是我尝试的方法,但是我遇到TypeError: 'in <string>' requires string as left operand ,如果只有一个条件,我可以打印它,但是如果有两个条件,我不确定该怎么做。

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. 但是, pos的元素必须是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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