将数据从url排列到列表中

时间:2017-02-09 16:31:43

标签: urllib2 urllib python-3.5

以下代码是用python 2编写的。如何在python 3中编写代码?感谢

import urllib2
import sys

#read data from uci data repository
target_url = ("https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data")

data = urllib2.urlopen(target_url)

#arrange data into list for labels and list of lists for attributes
xList = []
labels = []
for line in data:
#split on comma
    row = line.strip().split(",")
    xList.append(row)

1 个答案:

答案 0 :(得分:0)

您可以使用Python 3的requests库。

import requests
data = requests.get("https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data").text
for line in data.split('\n'):
    row = line.strip().split(",")
    xList.append(row)