不要通过字符打印来填充字符

时间:2016-02-09 01:28:59

标签: python python-2.7 for-loop python-2.x

文件内容:

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

我的代码:

 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

删除if not逻辑后,输出如下:

它只逐行打印单个字符。

t
e
l
n
e
t

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

8
0


T
r
y
i
n
g

我不想要逐个字符打印。

1 个答案:

答案 0 :(得分:0)

问题是 read()将整个文件作为字符串返回。因此,你的循环

for line in read:

一次一个地遍历字符。最简单的变化是:

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