我只是想知道是否可以将本地数据文件(如我的谷歌驱动器上的 .xlsx 或 .csv 文件)加载到 Colaboratory 中?
9 回答
乍一看加载本地文件的示例让我有点困惑,因为没有地方指定文件路径。您需要做的就是复制并粘贴配方来解决这个问题,但要清楚:
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])))
将向您显示访问您刚刚上传的内容的密钥。
编辑以进一步澄清:字典uploaded
将具有所选文件名的键 - 因此,例如,如果您选择一个文件my_test.txt
,那么您将使用uploaded['my_test.txt']
.
首先,执行这个单元格应该创建一个内联的“选择文件”按钮
from google.colab import files
uploaded = files.upload()
选择文件后,uploaded
将是键(文件名)和值(编码文件对象)的字典。要解码 Pandas 等库的文件,请尝试
import pandas as pd
import io
df = pd.read_csv(io.StringIO(uploaded['filename.csv'].decode('utf-8')))
在此之后,您的数据框df
应该准备好了
要将本地数据文件加载到 Colab:
方法一:谷歌驱动方法
- 将数据文件从系统内存上传到 Google 驱动器。
在 Colab 中挂载 Google 驱动器
from google.colab import drive drive.mount('/content/gdrive')
然后->
path = "/gdrive/My Drive/filename"
您现在可以在 Google Colab 中访问 Google Drive 文件。
方法二:直接加载
from google.colab import files
def getLocalFiles():
_files = files.upload()
if len(_files) >0:
for k,v in _files.items():
open(k,'wb').write(v)
getLocalFiles()
方法三:使用导入文件
from google.colab import files
uploaded = files.upload()
这是一个两步的过程。
第 1 步:首先使用以下代码在您的 colab 笔记本中调用文件选择器
from google.colab import files
uploaded = files.upload()
这将带您进入文件浏览器窗口
第 2 步:要将文件的内容加载到 Pandas 数据框中,请使用以下代码
import pandas as pd
import io
df = pd.read_csv(io.StringIO(uploaded['iris.csv'].decode('utf-8')))
print(df)
Putting this out there as an alternative for people who prefer another way to upload more files - this basically allows you to upload your files through Google Drive.
Run the below code (found this somewhere previously but I can't find the source again - credits to whoever wrote it!):
!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}
Click on the first link that comes up which will prompt you to sign in to Google; after that another will appear which will ask for permission to access to your Google Drive.
Then, run this which creates a directory named 'drive', and links your Google Drive to it:
!mkdir -p drive
!google-drive-ocamlfuse drive
If you do a !ls
now, there will be a directory drive
, and if you do a !ls drive
you can see all the contents of your Google Drive.
So for example, if I save my file called abc.txt
in a folder called ColabNotebooks
in my Google Drive, I can now access it via a path drive/ColabNotebooks/abc.txt
要从您的系统获取数据到 colab,请尝试以下操作:
from google.colab import files
uploaded = files.upload()
选择您要上传的文件并按回车键并完成。例如,我上传了一张图片并使用以下代码显示它:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image.jpg')
img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()
您可以使用此 URL 在 Google Colab 中上传文件:
https://colab.research.google.com/notebooks/io.ipynb#scrollTo=vz-jH8T_Uk2c
转到Local file system>Downloading files to your local file system
然后运行代码。之后,将出现浏览器按钮,供您从 PC 上传文件。
假设您在 Google 驱动器上有一个名为的文件夹,Colab
并且该csv
文件夹位于该文件夹中。加载此文件
import pandas as pd
titanic = pd.read_csv(“drive/Colab/Titanic.csv”)
titanic.head(5)
在此之前,您可能需要运行以下命令:
首先运行这些代码以安装必要的库并执行授权。
!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}
点击链接,复制验证码并粘贴到文本框中。
完成授权流程后,
挂载您的 Google 云端硬盘:
!mkdir -p drive
!google-drive-ocamlfuse drive