python - 输出不按行显示

标签 python multiple-columns

我写了一个简单的Python脚本,如下所示:

#!/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)

输出是这样的:

URL_PAGE                 STATUS
http://test.com
             200

但我想要这个输出:

URL_PAGE                 STATUS
http://test.com           200

最佳答案

url中去除换行符(和无用的空格):

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

关于python - 输出不按行显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467134/

相关文章:

python - 如何保持列表的一致性?

html - 固定最大高度时的 CSS 自动列数

python - 计算表格给定列中的所有字符串值并根据第三列对其进行分组

python - 如何按行连接包含字符串的多个列?

r - 尝试将多个连续数字列添加到 r 中的数据帧

Python 范围步长增加

python - 如何处理与 Pandas 数据框关联的元数据?

python - 如何计算名称列表中第二个字母为 'i' 的所有名称的出现次数

python - 使用 numpy 计算零空间的有理基

SQLite 条件 SELECT 语句 - 多个条件但单个语句