在Google Colaboratory中,URL重定向是否存在read_csv问题



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

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

然而,当我在谷歌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文件放在谷歌硬盘上,以免在第三方服务器上影响它。但我不能使用这个选项,因为我想使用的真正的foo.csv太大了,无法存储在我的谷歌硬盘上。

我为Colab找到了以下解决方案:!wget https://fonétik.fr/foo.csvpd.read_csv(foo.csv)

有时我也会遇到同样的问题所以我用这种方式它做得太多了(我知道!(但它有效只需替换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)

foo=pd.read_csv('https://raw.githubusercontent.com/user/repo/file.csv'(作品github.com的原始模式只是文件。

我还可以从媒体库中存储的博客网站上的URL中获取这些信息。

起初,我正在处理它,并将其保存在Colab实例的根目录中。这也是有效的,但这是一个额外的移动步骤,我后来发现没有必要。

如果数据集被压缩,您将不得不使用wget,然后!在细胞中拉开拉链,使其成为可用的形式。

最新更新