将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。
当前回答
快速,简单地从Dropbox导入:
!pip install dropbox
import dropbox
access_token = 'YOUR_ACCESS_TOKEN_HERE' # https://www.dropbox.com/developers/apps
dbx = dropbox.Dropbox(access_token)
# response = dbx.files_list_folder("")
metadata, res = dbx.files_download('/dataframe.pickle2')
with open('dataframe.pickle2', "wb") as f:
f.write(res.content)
其他回答
到目前为止,我发现的最简单的解决方案,适用于中小型CSV文件是:
在gi.github.com上创建一个秘密要点,然后上传(或复制粘贴)你的文件。 单击Raw视图并复制原始文件URL。 在调用pandas.read_csv(URL)时,使用复制的URL作为文件地址
这对于逐行读取文本文件或二进制文件可能有效,也可能无效。
如果你想在没有代码的情况下做到这一点,这很简单。 把你的文件夹压缩到我的箱子里
dataset.zip
然后在Colab中右键单击要放置此文件的文件夹,然后按上传并上传此zip文件。然后写这个Linux命令。
!unzip <your_zip_file_name>
您可以看到您的数据上传成功。
Dropbox的另一种简单方法是:
把你的数据放到dropbox里
复制文件的文件共享链接
那就去合作吧。
例如: ! wget - O文件名文件链接(如- https://www.dropbox.com/.....)
做完了。数据将开始出现在您的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)
在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。