获取以文件中特定字符串开头的行,并将其与python中的其他字符串进行比较

获取以文件中特定字符串开头的行,并将其与python中的其他字符串进行比较,python,python-2.7,Python,Python 2.7,解释我想做什么有点困难,但我会尽量表达清楚。我想在一个文件中查找一个字符串(url)并将其与另一个字符串(在我的代码中称为“服务”)进行比较,它实际上需要将所有服务与整个文件进行比较 如果文件中的url不包含服务,请对该url进行处理(从文件中删除该额外url)。 我的代码如下,但它没有给我正确的结果。我认为我的for循环有问题 def search_service(): status='Y' services = subprocess.Popen("docker-cloud ser

解释我想做什么有点困难,但我会尽量表达清楚。我想在一个文件中查找一个字符串(url)并将其与另一个字符串(在我的代码中称为“服务”)进行比较,它实际上需要将所有服务与整个文件进行比较 如果文件中的url不包含服务,请对该url进行处理(从文件中删除该额外url)。 我的代码如下,但它没有给我正确的结果。我认为我的for循环有问题

def search_service():
   status='Y'
   services = subprocess.Popen("docker-cloud service ps | awk '{print $1}'", shell=True, stdout=subprocess.PIPE)
   for line in iter(services.stdout.readline, ''):
       line=line.replace("\n","")
       with open('nginx.conf') as f:
           for word in f:
              if 'proxy_pass' in word:
                   st=word
           if re.search(line,st):
              status='Y'
           else:
              status='N'
              new=st
   if 'N' in status:
      print(st)           
      #remove_block(st)
DockerCloud输出服务的一个示例:

dev-qwerty
test-asdfgh
nginx.conf:

server {
       listen      80;
       server_name     asdfgh-test.example.com;
       location / {
          proxy_pass         http://test-asdfgh-example.io:5002/;
          proxy_redirect     off;

          ##proxy_set_header   Host             $host;
          blah
       }
}

 server {
       listen      80;
       server_name     nginx-dev.example.com;
       location / {
          proxy_pass         http://dev-nginx-example.io:5002/;
          proxy_redirect     off;

          ##proxy_set_header   Host             $host;
          blah
       }
}

server {
       listen      80;
       server_name     qwerty-dev.example.com;
       location / {
          proxy_pass         http://dev-qwerty.io:5106/;
          proxy_redirect     off;

          ##proxy_set_header   Host             $host;
          blah
       }
}  
预期的结果是:

proxy_pass         http://dev-nginx-example.io:5002/;

任何帮助都将不胜感激。

我不能完全确定我是否理解了你的要求,但这是否符合这些原则

dummy_input_file = 'dummy_file.in' 

with open(dummy_input_file, 'w') as out_file:
    out_file.write("""\
string1
string2
string3""")

expected_sub_strings = ['ing2', 'ing3']

modified_file_name = 'modified_file.out'
with open(dummy_input_file, 'r') as in_file, open(modified_file_name, 'w') as out_file:
    for line in in_file:
        if any(s in line for s in expected_sub_strings):
            out_file.write(line)

with open(modified_file_name, 'r') as in_file:
    print(in_file.read())
其中打印:

string2
string3

您的代码过于复杂,这与此相同,并且更易于阅读:

def search_service():
    services = subprocess.Popen("docker-cloud service ps | awk '{print $1}'", shell=True, stdout=subprocess.PIPE)
    docker_services = []
    for line in iter(services.stdout.readline, ''):
        line=line.replace("\n","")
        docker_services.append(line)

    with open('nginx.conf') as f:
       for word in f:
            if 'proxy_pass' in word:
                unknown = True
                for service in docker_services:
                    if service in word:
                        unknown = False
                        break

                if unknown:
                    print(word.strip())

首先,您可能应该检查粘贴的代码,缩进看起来是错误的。其次,如果你能发布一个你的
nginx.conf
的例子,以及docker云服务ps | awk'{print$1}'@uvesten的输出,这将非常有帮助。谢谢你的评论。我已经编辑了这个问题并包含了输出。如果您在上面将行
如果有(…
更改为
如果没有(…
),那么这就是您想要的。啊,您希望docker的输出中没有服务。这样就可以了。非常感谢,它工作得很好:)然后我需要使用一个数组,这并不是真正的原始问题…您的代码将始终检查
nginx.conf
中的最后一个服务,如果
与docker服务之一不匹配(这将始终是真的),那么您就打印它。检查代码中循环中的缩进以找出它。