Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jupyter中获取数据时在Python中接收NameError_Python_Jupyter_Nameerror - Fatal编程技术网

在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”)