Print line containing "word" python

41,664

Solution 1

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

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.

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

Solution 2

for single_line in websiteheaders.splitlines():
    if `Server` in single_line:
        print single_line
Share:
41,664
bryanvan
Author by

bryanvan

Updated on February 08, 2020

Comments

  • bryanvan
    bryanvan over 4 years

    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.

    Please help and thanks!

    EDIT: My complete code so far is pasted here: http://pastebin.com/sYuZyvX9 EDIT2: For completeness, in hosts.txt there currently is 1 host named "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.

    Using

    websiteheaders.extend(headers.splitlines())

    instead of

    websiteheaders.append(headers)

    did the trick for me. Thanks alot guys.