打印包含“word”python 的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13905101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:54:20  来源:igfitidea点击:

Print line containing "word" python

pythonpattern-matchingmatch

提问by bryanvan

I would like to print ONLY the line which contains "Server" in the below piece of output:

我只想在下面的输出中打印包含“服务器”的行:

Date: Sun, 16 Dec 2012 20:07:44 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=da8d52b67e5c7522:FF=0:TM=1355688464:LM=1355688464:S=CrK5vV-qb3UgWUM1; expires=Tue, 16-Dec-2014 20:07:44 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=nICkwXDM6H7TNQfHbo06FbvZhO61bzNmtOn4HA71ukaVDSgywlBjBkAR-gXCpMNo1TlYym-eYMUlMkCHVpj7bDRwiHT6jkr7z4dMrApDuTk_HuTrZrkoctKlS7lXjz9a; expires=Mon, 17-Jun-2013 20:07:44 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Connection: close

This information is fetched from a list called websiteheaders. I have the below piece of code which is driving me crazy that it is not working properly...

此信息是从名为 websiteheaders 的列表中获取的。我有下面的一段代码,这让我发疯,它不能正常工作......

for line in websiteheaders:
    if "Server" in line:
        print line

Now this above piece of code prints exactly the same block of text that is described at the beginning of my post. I just dont seem to get why it does that...

现在,上面这段代码打印出与我文章开头描述的完全相同的文本块。我只是似乎不明白为什么它会这样做......

As I've said, I only want to print the line that contains "Server", if possible without regex. And if not possible, with regex.

正如我所说,如果可能,我只想打印包含“服务器”的行,而不使用正则表达式。如果不可能,使用正则表达式。

Please help and thanks!

请帮助和感谢!

EDIT: My complete code so far is pasted here: http://pastebin.com/sYuZyvX9EDIT2: For completeness, in hosts.txt there currently is 1 host named "google.com"

编辑:到目前为止,我的完整代码粘贴在这里:http://pastebin.com/sYuZyvX9 EDIT2:为了完整起见,在 hosts.txt 中,目前有 1 个名为“google.com”的主机

Update

更新

My code was actually working fine, but there was a mistake in a other piece of my code which ensured that the data that was put into the list websiteheaders was 1 large string instead of multiple entries. In the above piece of code, it will ofcourse find "Server" and print the whole entry, which in my case was the full (large) string.

我的代码实际上运行良好,但是我的另一段代码中存在一个错误,该错误确保放入列表网站标题中的数据是 1 个大字符串,而不是多个条目。在上面的一段代码中,它当然会找到“服务器”并打印整个条目,在我的例子中是完整的(大)字符串。

Using

使用

websiteheaders.extend(headers.splitlines())

websiteheaders.extend(headers.splitlines())

instead of

代替

websiteheaders.append(headers)

websiteheaders.append(headers)

did the trick for me. Thanks alot guys.

帮我解决了这个问题。非常感谢各位。

回答by Niclas Nilsson

Is websiteheadersreally a list which is split for very line? Because if it's a string you should use:

websiteheaders真的被拆分很行的列表?因为如果它是一个字符串,你应该使用:

for line in websiteheaders.splitlines():
    if "Server" in line:
        print line

Also, a good tip: I would recommend adding some print-statements on encountering this kind of problems. If you would have added something like:

另外,一个很好的提示:我建议print在遇到此类问题时添加一些-statements。如果您添加了以下内容:

else:
    print 'WRONG LINE:', line

You probably would have catched that this loop was not looping over every line but over every character.

您可能会发现这个循环不是在每一行上循环,而是在每个字符上循环。

Update

更新

I can't wee what's wrong with your code then. This is what I get:

我不知道你的代码有什么问题。这就是我得到的:

In [3]: websiteheaders
Out[3]: 
['Date: Sun, 16 Dec 2012 20:07:44 GMT',
 'Expires: -1',
 'Cache-Control: private, max-age=0',
 'Content-Type: text/html; charset=ISO-8859-1',
 'Set-Cookie: PREF=ID=da8d52b67e5c7522:FF=0:TM=1355688464:LM=1355688464:S=CrK5vV-qb3UgWUM1; expires=Tue, 16-Dec-2014 20:07:44 GMT; path=/; domain=.google.com',
 'Set-Cookie: NID=67=nICkwXDM6H7TNQfHbo06FbvZhO61bzNmtOn4HA71ukaVDSgywlBjBkAR-gXCpMNo1TlYym-eYMUlMkCHVpj7bDRwiHT6jkr7z4dMrApDuTk_HuTrZrkoctKlS7lXjz9a; expires=Mon, 17-Jun-2013 20:07:44 GMT; path=/; domain=.google.com; HttpOnly',
 'P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."',
 'Server: gws',
 'X-XSS-Protection: 1; mode=block',
 'X-Frame-Options: SAMEORIGIN',
 'Connection: close"']

In [4]: for line in websiteheaders:
   ...:     if 'Server' in line:
   ...:         print line
   ...:         
Server: gws

回答by Skyler

for single_line in websiteheaders.splitlines():
    if `Server` in single_line:
        print single_line