簡體   English   中英

逐行讀取文件並打印包含兩個不同字符串的文件

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

因此,我有一個名為“ clears”的文件,我想查看一行是否包含列表中的兩個不同的字符串,如果要打印這些行,則無法正常工作。

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

我也試過f

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

那就是我嘗試的方法,但是我遇到TypeError: 'in <string>' requires string as left operand ,如果只有一個條件,我可以打印它,但是如果有兩個條件,我不確定該怎么做。

謝謝

我想你是說這個(編輯,回到第一個版本):

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

但是, pos的元素必須是字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM