colab研究谷歌:我应该如何将jupyter笔记本的输出写入我的谷歌硬盘

时间:2018-03-17 05:41:11

标签: python google-drive-api google-colaboratory

我正在为维基百科编写一个爬虫,并希望在csv中编写填充结果。 无论如何,我可以直接将生成的程序输出存储到Google电子表格中吗?

2 个答案:

答案 0 :(得分:0)

您可以使用google-drive-ocamlfusehttps://github.com/astrada/google-drive-ocamlfuse)在Google Colab实例上安装Google云端硬盘。要安装ocamlfuse并获取必要的权限,请执行:

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} 

在此之后,您需要使用以下命令将google驱动器安装到您的实例

!mkdir -p drive
!google-drive-ocamlfuse drive

现在您应该将所有的Google驱动器文件放在drive文件夹中。您可以使用以下方式查看:

!ls drive

在此之后,您可以在谷歌硬盘中读取或写入任何文件。

[1] https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d

答案 1 :(得分:0)

使用以下代码段连接到Gdrive。

您必须使用单元格输出中的链接进行两次身份验证。

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse

from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

!mkdir -p drive
!google-drive-ocamlfuse drive

使用pandas

读取CSV
df = pd.read_csv('drive/path/file.csv')

保存CSV

如果您不需要索引作为csv中的第一个col。

,请使用index = False
df.to_csv('drive/path/file.csv',index = False)