包含"word"的打印行Python [英] Print line containing "word" python

查看:105
本文介绍了包含"word"的打印行Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在下面的输出中打印包含"Server"的行:

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...

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.

请帮助并谢谢!

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

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

更新

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

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.

使用

websiteheaders.extend(headers.splitlines())

websiteheaders.extend(headers.splitlines())

代替

websiteheaders.append(headers)

websiteheaders.append(headers)

帮了我大忙.谢谢大家.

did the trick for me. Thanks alot guys.

推荐答案

websiteheaders真的是一个列表吗?因为如果是字符串,则应使用:

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

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

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

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

else:
    print 'WRONG LINE:', line

您可能会发现,此循环不是遍历每行而是遍历每个字符.

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

那我不知道您的代码出了什么问题.这就是我得到的:

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

这篇关于包含"word"的打印行Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆