【问题标题】:dont want character by character printing不想逐字打印
【发布时间】:2016-02-09 01:28:58
【问题描述】:

文件内容:

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

我不想逐个字符打印。

【问题讨论】:

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


    【解决方案1】:

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

    for line in read:
    

    遍历字符,一次一个。最简单的改变是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多