将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。
当前回答
下面是一种从谷歌驱动器导入文件到笔记本电脑的方法。
打开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")
我希望这对你们有帮助
其他回答
已解决,请在这里找到详细信息,并使用下面的功能: https://stackoverflow.com/questions/47212852/how-to-import-and-read-a-shelve-or-numpy-file-in-google-colaboratory/49467113#49467113
from google.colab import files
import zipfile, io, os
def read_dir_file(case_f):
# author: yasser mustafa, 21 March 2018
# case_f = 0 for uploading one File and case_f = 1 for uploading one Zipped Directory
uploaded = files.upload() # to upload a Full Directory, please Zip it first (use WinZip)
for fn in uploaded.keys():
name = fn #.encode('utf-8')
#print('\nfile after encode', name)
#name = io.BytesIO(uploaded[name])
if case_f == 0: # case of uploading 'One File only'
print('\n file name: ', name)
return name
else: # case of uploading a directory and its subdirectories and files
zfile = zipfile.ZipFile(name, 'r') # unzip the directory
zfile.extractall()
for d in zfile.namelist(): # d = directory
print('\n main directory name: ', d)
return d
print('Done!')
Dropbox的另一种简单方法是:
把你的数据放到dropbox里
复制文件的文件共享链接
那就去合作吧。
例如: ! wget - O文件名文件链接(如- https://www.dropbox.com/.....)
做完了。数据将开始出现在您的colab内容文件夹中。
这允许您通过谷歌驱动器上传您的文件。
运行下面的代码(之前在某个地方找到了这个,但我再也找不到源代码了——归功于写它的人!):
!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}
点击出现的第一个链接,它会提示你登录谷歌;之后,另一个将出现,将要求访问您的谷歌驱动器的权限。
然后,运行这个,创建一个名为“drive”的目录,并将您的谷歌drive链接到它:
!mkdir -p drive
!google-drive-ocamlfuse drive
如果您现在执行!ls,将会有一个目录驱动器,如果您执行!ls驱动器,您可以看到谷歌驱动器的所有内容。
例如,如果我将我的文件abc.txt保存在我的谷歌驱动器的一个名为ColabNotebooks的文件夹中,我现在可以通过路径驱动器/ColabNotebooks/abc.txt访问它
在Colab中只有两行代码。非常简单的方法:
将您的所有文件装入一个压缩档案谷歌驱动器。 通过链接让每个人都能看到。 从这个链接复制ID。(例如:在这个链接中https://drive.google.com/open?id=29PGh8XCts3mlMP6zRphvnIcbv27boawn ID是29PGh8XCts3mlMP6zRphvnIcbv27boawn) 进入Colab: !gdown——id 29pgh8xcts3mlmp6zrphvnicbv27bown 最后一步进入Colab: ! 解压缩file_name.zip
瞧!Colab中/content/file_name.csv中所有需要的文件都已准备就绪
对于这个简单的方法从驱动器到Colab,我感谢Gleb Mikhaylov。
下面是一种从谷歌驱动器导入文件到笔记本电脑的方法。
打开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")
我希望这对你们有帮助