Aditya Desai February 2016

dont want character by character printing

File content:

aditya@aditya-virtual-machine:~/urlcat$ cat http_resp
telnet 10.192.67.40 80
Trying 10.192.67.40...
Connected to 10.192.67.40.
Escape character is '^]'.
GET /same_domain HTTP/1.1
Host: www.google.com

HTTP/1.1 200 OK
Date: Tue, 09 Feb 2016 00:25:36 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Fri, 08 Jan 2016 20:10:52 GMT
ETag: "81-528d82f2644f1"
Accept-Ranges: bytes
Content-Length: 129

My code:

 f1 = open('http_resp')
    read=f1.read()
    for line in read:
        #     line=line.rstrip()
             line=line.strip()
             if not '.com' in line:
                continue
             print line

When if not logic is removed, the output is something like this:

it prints only single character line by line.

t
e
l
n
e
t

1
0
.
1
9
2
.
6
7
.
4
0

8
0


T
r
y
i
n
g

I don't want character-by-character printing.

Answers


Prune February 2016

The problem is that read() returns the entire file as a string. Thus, your loop

for line in read:

iterates through the characters, one at a time. The simplest change is this:

f1 = open('http_resp')
for line in f1.readlines():

Post Status

Asked in February 2016
Viewed 3,290 times
Voted 13
Answered 1 times

Search




Leave an answer