Output isn't shown per line

假装没事ソ 提交于 2020-01-05 06:46:06

问题


I wrote a simple python script like this:

#!/usr/bin/python
import sys
import urllib

if len(sys.argv) < 2:
        print 'usage: python %s <file-urls>' % (sys.argv[0])
        sys.exit(2)

print '%-15s %15s' % ('URL_PAGE', 'STATUS')

FileName = sys.argv[1]
InputFile = open(FileName)
for url in InputFile:
    out = urllib.urlopen(url)
    status = out.getcode()
    print '%-15s %15s' % (url, status)

The out put is something like this:

URL_PAGE                 STATUS
http://test.com
             200

But I want this output:

URL_PAGE                 STATUS
http://test.com           200

回答1:


strip the newline character (and useless whitespace) from url:

print '%-15s %15s' % (url.strip(), status)


来源:https://stackoverflow.com/questions/24467134/output-isnt-shown-per-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!