2016-02-09 100 views
1

文件內容要角色:不按字符打印

[email protected]:~/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 

我不希望逐字符打印。

回答

0

問題是,read()將整個文件作爲字符串返回。因此,你的循環

for line in read: 

遍歷字符,一次一個。最簡單的變化是:

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