【问题标题】:colab research google: How should i write the output of the jupyter notebook to my google drivecolab research google:我应该如何将 jupyter notebook 的输出写入我的谷歌驱动器
【发布时间】:2018-08-26 05:41:15
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您可以使用 google-drive-ocamlfuse (https://github.com/astrada/google-drive-ocamlfuse) 将您的 google 驱动器安装到 google colab 实例上。要安装 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} 
    

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

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

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

    !ls drive
    

    在此之后,您可以在您的 google 驱动器中读取或写入任何文件。

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

    【讨论】:

      【解决方案2】:

      使用下面的 sn-p 连接到 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 中的第一个列,请使用 index = False。

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

      【讨论】:

        猜你喜欢
        • 2022-07-23
        • 2021-03-25
        • 2018-07-19
        • 2018-11-04
        • 2019-07-12
        • 2019-12-24
        • 2020-03-20
        • 2020-09-18
        • 2021-06-13
        相关资源
        最近更新 更多