我想从网上下载一个文件,即:http://www.malwaredomainlist.com/hostslist/ip.txt并将其放入列表中以进一步操作列表中的项目。
我试过
print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
data = [f.read()]
result = [stuff.replace("\r\n", "/32,") for stuff in data]
print result
print len([result])
列表本身"看起来"细:
['100.42.50.110/32,103.14.120.121/32,.......']
但长度仅为1 我想我需要遍历readlines并在列表中为每条readline创建项目,对吗?
答案 0 :(得分:2)
我认为你过于复杂了:
print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read().split("\r\n")
# If you want to add '/32' to each IP
ips = [x + "/32" for x in ips if x]
答案 1 :(得分:0)
>>> r=urllib2.urlopen('http://www.malwaredomainlist.com/hostslist/ip.txt')
>>> f=open('e.txt','w')
>>> ff=r.read()
>>> f.write(ff)
>>> f.close()
答案 2 :(得分:0)
list1 = []
print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read()
for ip in ips:
ip = ip.strip()
ip = ip + "/32"
list1.append(ip)
print list1
print len(list1)