I have a file that has a repeated number of blocks of different sizes as in below:

ID: 2
Time: 76
Op: GET
Domain: facebook.com
LPath: /common/css/common.css
Referer: http://google.com
User-Agent: Mozilla/4.0

ID: 3
Op: GET
Domain: rfi.fr
LPath: /common/css/common.css
Referer: http://yahoo.com

For each block, I want to print only the lines that start with 'Domain' and 'Referer'. This is how I approached the problem:

f = open('testconn.txt').read()
lines = ["Conn:" , "Domain:" , "Referer"]

for record in f.split('\n\n'):
    for i in record:
        if record in lines:
	    print record

It gives no error and prints nothing either. Any help is appreciated.

for line in open('testconn.txt'):
    if line.startswith('Domain') or line.startswith('Referer'):
       print line

Usually you should also strip the record first, as no data should be assumed to be 100% reliable. You may or may not want to convert to lower case as well.

for line in open('testconn.txt'):
    rec = line.strip()
    if rec.startswith('Domain') or rec.startswith('Referer'):
       print line

Then you can compress woooee answer to single line if you like:

test = """ID: 2
Time: 76
Op: GET
Domain: facebook.com
LPath: /common/css/common.css
Referer: http://google.com
User-Agent: Mozilla/4.0

ID: 3
Op: GET
Domain: rfi.fr
LPath: /common/css/common.css
Referer: http://yahoo.com
"""

print '\n'.join(line for line in test.splitlines()
                if any(line.startswith(word) for word in ('Domain', 'Referer')))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.