贾坤

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

下载数据

import os

import tarfile # 用于压缩和解压文件
import urllib

DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = "datasets/housing"
HOUSING_URL = DOWNLOAD_ROOT + HOUSING_PATH + "/housing.tgz"


# 下载数据
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
if not os.path.isdir(housing_path):
os.makedirs(housing_path)
tgz_path = os.path.join(housing_path, "housing.tgz")
# urlretrieve()方法直接将远程数据下载到本地
urllib.request.urlretrieve(housing_url, tgz_path)
housing_tgz = tarfile.open(tgz_path)
housing_tgz.extractall(path=housing_path) # 解压文件到指定路径,不指定就是解压到当前路径
housing_tgz.close()
fetch_housing_data()

posted on 2020-11-21 17:43  贾坤  阅读(188)  评论(0)    收藏  举报