下面的代码是在差异行中打印输出。我想要 -->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/