This question already has answers here:
How to readlines() from urllib
(2个答案)
3年前关闭。
我只是从python 2.7开始,然后尝试执行一个相当基本的数据操作任务。
目标:从网站上获取列表并逐行处理数据。
但是我跌倒了第二关。将数据放入一个数组,稍后可以对其进行操作。
我尝试了几种方法,而不是每行处理每个数组条目,而是每个字符处理一个数组条目。这是我到目前为止尝试过的方法:
输出:
我在列表变量中都尝试了
我知道我缺少一些基本的东西,因为
另外,如果您注意到的话,我将
同样,基于@Lukas的注释,您可以将代码重构为:
(2个答案)
3年前关闭。
我只是从python 2.7开始,然后尝试执行一个相当基本的数据操作任务。
目标:从网站上获取列表并逐行处理数据。
但是我跌倒了第二关。将数据放入一个数组,稍后可以对其进行操作。
我尝试了几种方法,而不是每行处理每个数组条目,而是每个字符处理一个数组条目。这是我到目前为止尝试过的方法:
import urllib2, numpy
from array import array
listraw = urllib2.urlopen("https://zeustracker.abuse.ch/blocklist.php?download=badips").read()
list = [line.rstrip('\n\r') for line in listraw]
array = []
for line in listraw:
array.append(line)
numpyarray = numpy.asarray(listraw)
lines = tuple(listraw)
#print r'Listraw:'
#print listraw
print 'Single List item: '
print list [10]
print array[10]
print lines[10]
print numpyarray[10]
输出:
Single List item:
#
#
#
Traceback (most recent call last):
File "./test.py", line 17, in <module>
print numpyarray[10]
IndexError: too many indices for array
我在列表变量中都尝试了
\n
和\r
独奏,但没有成功。如果我取消注释print listraw
,则会在正确的位置显示带有回车符的整个列表。我知道我缺少一些基本的东西,因为
array2= ["bob","bert","geof"]
可以工作,但是到目前为止我发现的所有内容都没有解决我的问题。实现目标的最佳方法是什么? 最佳答案
循环访问数据,您似乎将strip
和split
弄混了。尝试:
data_list = listraw.split('\n')
另外,如果您注意到的话,我将
list
重命名为data_list
,因为list
是python内置的,并为其分配了某些内容会覆盖它,这可能导致将来出现意外情况且难以跟踪错误。同样,基于@Lukas的注释,您可以将代码重构为:
listraw = urllib2.urlopen("https://zeustracker.abuse.ch/blocklist.php?download=badips")
array = []
for line in listraw:
array.append(line.strip())
numpyarray = numpy.asarray(listraw)
关于python - 通过Web流在Python中创建数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37602238/