在Jupyter中获取数据时在Python中接收NameError
这个脚本会给我一个名称错误,请问它有什么问题在Jupyter中获取数据时在Python中接收NameError,python,jupyter,nameerror,Python,Jupyter,Nameerror,这个脚本会给我一个名称错误,请问它有什么问题 import os import tarfile from six.moves import urllib DOWNLOAD_ROOT ="https://raw.githubusercontent.com/ageron/handson-ml/master/" HOUSING_PATH = os.path.join("datasets", "housing") HOUSING_URL = DOWNLOAD_ROOT + "datasets/hou
import os
import tarfile
from six.moves import urllib
DOWNLOAD_ROOT ="https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/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")
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()
这就是错误:
NameError Traceback (most recent call last)
tgz_path = os.path.join(housing_path, "housing.tgz")
NameError: name 'housing_path' is not defined
这看起来像是一个空白问题:以
tgz_path=…
开头的5行需要缩进,因此它们是fetch_housing_data()
函数的一部分
import os
import tarfile
from six.moves import urllib
DOWNLOAD_ROOT ="https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/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")
urllib.request.urlretrieve(housing_url, tgz_path)
housing_tgz = tarfile.open(tgz_path) # <-- not 'tgz.path'
housing_tgz.extractall(path=housing_path)
housing_tgz.close()
fetch_housing_data()
导入操作系统
导入tarfile
从六个移动导入urllib
下载_ROOT=”https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING\u PATH=os.PATH.join(“数据集”、“外壳”)
HOUSING\u URL=DOWNLOAD\u ROOT+“datasets/HOUSING/HOUSING.tgz”
def fetch_外壳_数据(外壳_url=外壳_url,外壳_路径=外壳_路径):
如果不是os.path.isdir(外壳路径):
os.makedirs(外壳路径)
tgz_path=os.path.join(housing_path,“housing.tgz”)
urllib.request.urlretrieve(包含url、tgz路径)
housing_tgz=tarfile.open(tgz_path)#由于housing_path
已在函数获取_housing_数据
之外,您无法再访问。将tgz\u path=os.path.join(housing\u path,“housing.tgz”)
替换为tgz\u path=os.path.join(housing\u path,“housing.tgz”)