将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。


当前回答

你也可以在谷歌上使用我的实现。colab和PyDrive在https://github.com/ruelj2/Google_drive,这使它更容易。

!pip install - U - q PyDrive  
import os  
os.chdir('/content/')  
!git clone https://github.com/ruelj2/Google_drive.git  

from Google_drive.handle import Google_drive  
Gd = Google_drive()  

然后,如果你想加载谷歌驱动器目录中的所有文件,只需

Gd.load_all(local_dir, drive_dir_ID, force=False)  

或者只是一个特定的文件

Gd.load_file(local_dir, file_ID)

其他回答

对于那些像我一样从谷歌搜索关键字“上传文件colab”的人:

from google.colab import files
uploaded = files.upload()

一个演示本地文件上传/下载以及与Drive和sheets集成的官方示例笔记本可在这里获得: https://colab.research.google.com/notebooks/io.ipynb

共享文件最简单的方法是挂载您的谷歌驱动器。

要做到这一点,在代码单元格中运行以下命令:

from google.colab import drive
drive.mount('/content/drive')

它会要求您访问一个链接,以允许“谷歌文件流”访问您的驱动器。之后,一个长长的字母数字认证代码将显示,需要输入在你的Colab的笔记本。

之后,您的驱动器文件将被挂载,您可以在侧面板中的文件浏览器浏览它们。

这是一个完整的笔记本示例

简单的方法从你的googledrive导入数据-这样做节省了人们的时间(不知道为什么谷歌只是没有明确地列出这一步)。

安装并验证pydrive

     !pip install -U -q PyDrive ## you will have install for every colab session

     from pydrive.auth import GoogleAuth
     from pydrive.drive import GoogleDrive
     from google.colab import auth
     from oauth2client.client import GoogleCredentials

     # 1. Authenticate and create the PyDrive client.
     auth.authenticate_user()
     gauth = GoogleAuth()
     gauth.credentials = GoogleCredentials.get_application_default()
     drive = GoogleDrive(gauth)

上传

如果您需要从本地驱动器上传数据:

    from google.colab import files

    uploaded = files.upload()

    for fn in uploaded.keys():
       print('User uploaded file "{name}" with length {length} bytes'.format(name=fn, length=len(uploaded[fn])))

执行,这将显示一个选择文件按钮-找到你的上传文件-点击打开

上传完成后,会显示:

    sample_file.json(text/plain) - 11733 bytes, last modified: x/xx/2018 - %100 done
    User uploaded file "sample_file.json" with length 11733 bytes

为笔记本创建文件

如果您的数据文件已经在您的gdrive中,您可以跳过这一步。

现在它在你的谷歌硬盘里。在谷歌驱动器中找到该文件,然后右键单击。点击获取“可共享链接”。你会得到一个窗口,上面有:

    https://drive.google.com/open?id=29PGh8XCts3mlMP6zRphvnIcbv27boawn

Copy - ' 29pgh8xcts3mlmp6zrphvnicbv27bown ' -这是文件ID。

在你的笔记本上:

    json_import = drive.CreateFile({'id':'29PGh8XCts3mlMP6zRphvnIcbv27boawn'})

    json_import.GetContentFile('sample.json') - 'sample.json' is the file name that will be accessible in the notebook.

将数据导入笔记本

导入你上传到笔记本的数据(在这个例子中是一个json文件-你如何加载取决于文件/数据类型- .txt,.csv等):

    sample_uploaded_data = json.load(open('sample.json'))

现在你可以打印数据:

    print(sample_uploaded_data)

我创建了一小段代码,可以以多种方式实现这一点。你可以

使用已经上传的文件(在重新启动内核时很有用) 使用来自Github的文件 手动上传文件

import os.path

filename = "your_file_name.csv"
if os.path.isfile(filename):
  print("File already exists. Will reuse the same ...")
else:
  use_github_data = False  # Set this to True if you want to download from Github
  if use_github_data:
    print("Loading fie from Github ...")
    # Change the link below to the file on the repo
    filename = "https://github.com/ngupta23/repo_name/blob/master/your_file_name.csv" 
  else:
    print("Please upload your file to Colab ...")
    from google.colab import files
    uploaded = files.upload()

下面是一种从谷歌驱动器导入文件到笔记本电脑的方法。

打开jupyter notebook并运行下面的代码并完成身份验证过程

!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

从谷歌驱动器导入文件到笔记本(例如:Colab_Notebooks/db.csv)

假设你的数据集文件在Colab_Notebooks文件夹中,它的名字是db.csv

import pandas as pd
dataset=pd.read_csv("drive/Colab_Notebooks/db.csv")

我希望这对你们有帮助