python | 学习总结 urllib.request

2019-02-18 17:41:14 浏览数 (1)

在anaconda3下,用urllib.request包来获取数据

例子:计算行数、列数

代码语言:javascript复制
__author__ = 'zhangx'
import urllib.request
import sys


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

data = urllib.request.urlopen(target_url)


#转为列表数据
xList = []
labels = []
for line in data:
    #split on comma
    row = line.strip().decode('utf-8').split(",") #注意编码方式
    xList.append(row)


#输出
sys.stdout.write("Number of Rows of Data ="   str(len(xList))   'n')
sys.stdout.write("Number of Columns of Data ="   str(len(xList[1])))

Number of Rows of Data =208 Number of Columns of Data =61

总结和进步

0 人点赞