将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。
当前回答
这允许您通过谷歌驱动器上传您的文件。
运行下面的代码(之前在某个地方找到了这个,但我再也找不到源代码了——归功于写它的人!):
!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访问它
其他回答
快速,简单地从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)
如果数据集大小小于25mb,最简单的方法是从你的GitHub存储库上传CSV文件。
单击存储库中的数据集 点击“查看原始”按钮 复制链接并将其存储在一个变量中 将变量加载到Pandas read_csv中以获得数据帧
例子:
import pandas as pd
url = 'copied_raw_data_link'
df1 = pd.read_csv(url)
df1.head()
在谷歌colabs 如果这是你第一次
from google.colab import drive
drive.mount('/content/drive')
运行这些代码并遍历输出链接 然后穿过传送带到达箱子
复制时,你可以按照下面的方法复制, 转到文件右键单击并复制路径 ***不要忘记删除" /content "
f = open("drive/My Drive/RES/dimeric_force_field/Test/python_read/cropped.pdb", "r")
这允许您通过谷歌驱动器上传您的文件。
运行下面的代码(之前在某个地方找到了这个,但我再也找不到源代码了——归功于写它的人!):
!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访问它
您可以使用下面的函数。我假设您正在尝试上传一个数据帧类型的文件(.csv, .xlsx)
def file_upload():
file = files.upload()
path = f"/content/{list(file.keys())[0]}"
df = pd.read_excel(path)
return df
#your file will be saved in the variable: dataset
dataset = file_upload()
这是在你没有改变谷歌合作目录的情况下,这是最简单的方法