Python 将本地数据文件加载到 Colaboratory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47320052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:09:33  来源:igfitidea点击:

Load local data files to Colaboratory

pythongoogle-colaboratory

提问by northcheng

I just wondering that is it possible to load local data files(like .xlsx or .csv files that on my google drive) into Colaboratory?

我只是想知道是否可以将本地数据文件(如我的谷歌驱动器上的 .xlsx 或 .csv 文件)加载到 Colaboratory 中?

回答by elz

I was a bit confused by the example for loading local files on first glance as there was no place to specify a file path. All you need to do is copy and paste the recipeto figure this out, but to be clear:

乍一看,我对加载本地文件的示例感到有些困惑,因为没有地方可以指定文件路径。您需要做的就是复制并粘贴配方来解决这个问题,但要清楚:

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

will open an upload dialogue window where you can browse and select your local files for upload.

将打开一个上传对话窗口,您可以在其中浏览并选择要上传的本地文件。

Then

然后

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

will show you the keys to access what you just uploaded.

将向您显示访问您刚刚上传的内容的密钥。

Edit for additional clarification: The dictionary uploadedwill have keys of the selected filenames - so if for example you select a file my_test.txt, then you would access that file using uploaded['my_test.txt'].

编辑以获取更多说明:字典uploaded将包含所选文件名的键 - 因此,例如,如果您选择一个文件my_test.txt,那么您将使用uploaded['my_test.txt'].

回答by Bob Smith

Yes, all of these scenarios are supported.

是的,所有这些场景都受支持。

For recipes to access local and Drive files, check out the I/O example notebook.

有关访问本地和 Drive 文件的方法,请查看I/O 示例笔记本

For access to xlsfiles, you'll want to upload the file to Google Sheets. Then, you can use the gspreadrecipes in the same I/O example notebook.

要访问xls文件,您需要将文件上传到 Google 表格。然后,您可以使用gspread同一I/O 示例笔记本中的配方。

A recently added way to upload local files is to use the 'Files' tab in the right hand side drawer.

最近添加的上传本地文件的方法是使用右侧抽屉中的“文件”选项卡。

enter image description here

在此处输入图片说明

From there, you can upload a local file using the 'upload' button.

从那里,您可以使用“上传”按钮上传本地文件。

enter image description here

在此处输入图片说明

(You can also download files by right clicking on them in the file tree.)

(您也可以通过在文件树中右键单击文件来下载文件。)

回答by Zachary Nagler

First, executing this cell should create an inline "Choose Files" button

首先,执行这个单元格应该创建一个内嵌的“选择文件”按钮

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

After selecting your file(s), uploadedwill be a dictionary of keys (the file names) and values (the encoded file objects). To decode the files for a library such as Pandas, try

选择文件后,uploaded将是键(文件名)和值(编码的文件对象)的字典。要解码 Pandas 等库的文件,请尝试

import pandas as pd
import io
df = pd.read_csv(io.StringIO(uploaded['filename.csv'].decode('utf-8')))

After this your dataframe dfshould be ready to go

在此之后,您的数据框df应该准备好了

回答by yl_low

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.

将其作为一种替代方法供喜欢以其他方式上传更多文件的人使用 - 这基本上允许您通过 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.

单击出现的第一个链接,该链接将提示您登录 Google;之后会出现另一个要求访问您的 Google Drive 的权限。

Then, run this which creates a directory named 'drive', and links your Google Drive to it:

然后,运行它,它会创建一个名为“drive”的目录,并将您的 Google Drive 链接到它:

!mkdir -p drive
!google-drive-ocamlfuse drive

If you do a !lsnow, there will be a directory drive, and if you do a !ls driveyou can see all the contents of your Google Drive.

如果你!ls现在做,会有一个目录drive,如果你做一个,!ls drive你可以看到你的谷歌驱动器的所有内容。

So for example, if I save my file called abc.txtin a folder called ColabNotebooksin my Google Drive, I can now access it via a path drive/ColabNotebooks/abc.txt

例如,如果我将我的文件保存在我的 Google Driveabc.txt中的文件夹ColabNotebooks中,我现在可以通过路径访问它drive/ColabNotebooks/abc.txt

回答by minakshi das

To get data from your system to colab try this:

要从您的系统获取数据以进行 colab,请尝试以下操作:

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

Choose the file you want to upload and hit enter and its done. For example, I have uploaded an image and displayed it using the code below:

选择您要上传的文件并按回车键完成。例如,我上传了一张图片并使用以下代码显示它:

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()

回答by shaurya uppal

To load local data files to Colab:

要将本地数据文件加载到 Colab:

Method 1: Google Drive Method

方法 1:Google Drive 方法

  1. Upload data file from system memory to Google drive.
  2. Mount Google drive in Colab

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

  3. Then-> path = "/gdrive/My Drive/filename"

  1. 将数据文件从系统内存上传到 Google Drive。
  2. 在 Colab 中挂载 Google 驱动器

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

  3. 然后-> path = "/gdrive/My Drive/filename"

You can now access google drive files in Google Colab.

您现在可以在 Google Colab 中访问 Google Drive 文件。

Method 2: Direct Load

方法二:直接加载

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()

Method 3: Using import files

方法 3:使用导入文件

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

回答by hamed baziyad

You can use this URL for uploading your files in Google Colab:

您可以使用此 URL 在 Google Colab 中上传文件:

https://colab.research.google.com/notebooks/io.ipynb#scrollTo=vz-jH8T_Uk2c

go to Local file system>Downloading files to your local file systemThen run the code. After that, browser button will be appeared for you to uploading your files from your PC.

转到Local file system>Downloading files to your local file system然后运行代码。之后,将出现浏览器按钮供您从 PC 上传文件。

回答by Sudarshan

Say, You have a folder on your Google drive named Colaband a csvis file located there. To load this file

假设您在 Google 驱动器上有一个名为的文件夹,Colab并且有一个csv文件位于那里。加载这个文件

import pandas as pd
titanic = pd.read_csv(“drive/Colab/Titanic.csv”)
titanic.head(5)

Before that, you may need to run these command:

在此之前,您可能需要运行以下命令:

Run these codes first in order to install the necessary libraries and perform authorization.

首先运行这些代码以安装必要的库并执行授权。

!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}

When you run the code above, you should see a result like this: enter image description here

当你运行上面的代码时,你应该看到这样的结果: 在此处输入图片说明

Click the link, copy verification code and paste it to text box.

单击链接,复制验证码并将其粘贴到文本框中。

After completion of the authorization process,

完成授权流程后,

mount your Google Drive:

挂载您的 Google 云端硬盘:

!mkdir -p drive
!google-drive-ocamlfuse drive

回答by Sri ram

It's a 2 step process.

这是一个 2 步过程。

Step 1 : First invoke a file selector with in your colab notebook with the following code

第 1 步:首先使用以下代码在您的 colab 笔记本中调用文件选择器

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

this will take you to a file browser window

这将带您进入文件浏览器窗口

step 2 : To load the content of the file into Pandas dataframe, use the following code

第 2 步:要将文件内容加载到 Pandas 数据帧中,请使用以下代码

import pandas as pd
import io
df = pd.read_csv(io.StringIO(uploaded['iris.csv'].decode('utf-8')))
print(df)