【问题标题】:Importing Data from github using Python in a Jupyter notebook在 Jupyter 笔记本中使用 Python 从 github 导入数据
【发布时间】:2020-02-10 22:00:50
【问题描述】:

我正在使用 Aurelien Geron 的“Hands-on machine learning with scikit-learn and tensorflow”一书。

这是我第一次使用 Jupyter 和 Python。

我正在尝试遵循以下代码。

我的问题是当我使用以下代码运行单元时:

import os
import tarfile
import urllib
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/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):
    os.makedirs(housing_path, exist_ok=True)
    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()

单元格评估永远不会结束,In[*]: 永远不会变成In[1]: 之类的东西。

所以,我认为这是初始 url 的问题,因为当我通过 Internet 浏览器访问它时显示错误。

因此,我将其更改为DOWNLOAD_ROOT = "https://github.com/ageron/handson-ml2/tree/master/"

现在我得到In[1]:。但是,当我运行 fetch_housing_data() 时,我得到:

---------------------------------------------------------------------------
ReadError                                 Traceback (most recent call last)
<ipython-input-6-bd66b1fe6daf> in <module>
----> 1 fetch_housing_data()

<ipython-input-5-ef3c39b342d8> in fetch_housing_data(housing_url, housing_path)
      9     tgz_path = os.path.join(housing_path, "housing.tgz")
     10     urllib.request.urlretrieve(housing_url, tgz_path)
---> 11     housing_tgz = tarfile.open(tgz_path)
     12     housing_tgz.extractall(path=housing_path)
     13     housing_tgz.close()

~\Anaconda3\lib\tarfile.py in open(cls, name, mode, fileobj, bufsize, **kwargs)
   1576                         fileobj.seek(saved_pos)
   1577                     continue
-> 1578             raise ReadError("file could not be opened successfully")
   1579 
   1580         elif ":" in mode:

ReadError: file could not be opened successfully

为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: python import jupyter-notebook


    【解决方案1】:

    您是否重新启动内核并尝试再次运行?
    您所看到的无法重现。

    您在上面粘贴的第一个代码块按照编写的方式工作。无需修改。
    我只是在下面运行了这个,然后当我在另一个单元格中运行 fetch_housing_data() 时它起作用了:

    import os
    import tarfile
    import urllib
    DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/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):
        os.makedirs(housing_path, exist_ok=True)
        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()
    

    您确定这不仅仅是您没有看到单元完成的人工制品吗?
    如果您想独立验证,您可以像我一样在其他地方运行它。我刚刚通过here 并按下底部的launch binder 链接对其进行了测试。然后我将您的代码粘贴到出现的单元格中。运行这两个单元后,我在/home/jovyan/scripts/datasets/housing 有一个目录,其中包含housing.csv housing.tgz 的内容。

    【讨论】:

    • 嗨,韦恩,感谢您的帮助。我再次运行代码,它现在可以工作了......我不知道为什么它不起作用。也许是因为我第一次安装了 Anaconda,而没有重新启动我的系统。我不知道...再次感谢 ;)
    【解决方案2】:

    https://raw.githubusercontent.com/ageron/handson-ml2/master/

    我不确定这是什么类型的链接。也许有人可以解释。当我只输入此链接时,我无法访问该页面。然而,它确实适用于检索我在下一段中解释的数据。如果我使用你上面提到的实际github链接https://github.com/ageron/handson-ml2/tree/master/,代码无法提取数据。

    通过在“导入”中添加另一行,我已经能够使用书中的步骤从链接中提取 csv 文件。我添加了“导入 urllib.request”。这似乎在 Google Colab 上对我有用。导入 urllib 你会认为 urllib.request 也被导入了,但事实并非如此。我无法回答为什么它会起作用,但 urllib 的文档在一个示例中有“import urllib.request”,我接受了这个想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2020-11-20
      • 2017-12-04
      • 2016-03-01
      相关资源
      最近更新 更多