首页 > 解决方案 > 在 Google Colaboratory 中,URL 重定向是否存在 read_csv 问题?

问题描述

在我笔记本电脑上的本地 Jupyter 笔记本中使用 pandas 打开以下 CSV 文件效果很好:

pd.read_csv('http://fonetik.fr/foo.csv')

但是,当我在 Google Colab 笔记本中尝试相同的代码行时,笔记本显示以下错误:

CertificateError                          Traceback (most recent call last)
<ipython-input-27-030762f24a0e> in <module>()

----> 1 df = pd.read_csv('http://fonetik.fr/foo.csv')
 /usr/lib/python3.6/ssl.py in match_hostname(cert, hostname)
   325         raise CertificateError("hostname %r "
   326             "doesn't match either of %s"
--> 327             % (hostname, ', '.join(map(repr, dnsnames))))
   328     elif len(dnsnames) == 1:
   329         raise CertificateError("hostname %r "

CertificateError: hostname 'fonétik.fr' doesn't match either of 'fonetik.fr', 'www.fonetik.fr', 'www.xn--fontik-dva.fr', 'xn--fontik-dva.fr'

我刚刚检查了fonetik.fr证书,它是有效的。因此,我不明白为什么 Jupyter Colab 会引发此错误。也许是因为 IDA 服务器和非 IDA 服务器之间的某种重定向?有解决方案吗?

您可能认为我应该先将 foo.csv 文件放在 Google Drive 上,以避免在第三方服务器上感染它。但是我不能使用这个选项,因为我想使用的真正的 foo.csv 很大而且太大而无法存储在我的 Google Drive 上。

标签: pythonpandasgoogle-colaboratoryidn

解决方案


有时我有同样的问题,所以我以这种方式使用它,它做的太多了(我知道!)但它只是替换你的 URL 和变量:

 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):
   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()
def load_housing_data(housing_path=Housing_path):
csv_path = os.path.join(housing_path, "housing.csv")
return pd.read_csv(csv_path)

推荐阅读