python - Unix下python程序导入文件

标签 python linux python-2.7 unix aix

下面的代码是在差异行中打印输出。我想要 -->200 但它显示在 diff 行中..

with open('URL.txt','r') as f:


    for url in f:
        print url  ## Printing URL with line gap
        ret = urllib2.urlopen(url)
        print url,"-->",ret.code

输出:

http://www.gogle.com
--> 200
http://yahoo.com --> 200

最佳答案

这将迭代文件的行。假设每一行都是一个 url,它会为每个 url 调用您的函数。

import urllib2
with open('URL.txt','r') as f:
    for url in f:
        ret = urllib2.urlopen(url)
        print("%s --> %s" % (url.strip(), ret.code))

编辑:添加了strip()以获得正确的输出

关于python - Unix下python程序导入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164115/

相关文章:

python - 从 Pox Controller 停止切换

linux - 在嵌入式 Linux 上安全写入紧凑型闪存

php - 传输后数据库无法连接到 Linux 服务器

python-2.7 - 尝试使用 gensim word2vec 时使用 a.all() 或 a.any() 错误

python - 如何在不使用 if 语句的情况下启用/禁用一行代码?

python - 仅将某些列 reshape 为单个列

linux - Ansible:ansible.config中的source profile.d

python - 验证字符串是否仅包含一系列数字 Python 2.7

python-2.7 - 如何在ubuntu中完全卸载python并重新安装?

python - 使用 functools.partial 进行跟踪