将私人数据导入谷歌协作笔记本的常用方法是什么?是否可以导入一个非公开的谷歌表?不能从系统文件中读取。介绍性文档链接到使用BigQuery的指南,但这似乎有点…多。


当前回答

如果你想在没有代码的情况下做到这一点,这很简单。 把你的文件夹压缩到我的箱子里

dataset.zip

然后在Colab中右键单击要放置此文件的文件夹,然后按上传并上传此zip文件。然后写这个Linux命令。

!unzip <your_zip_file_name>

您可以看到您的数据上传成功。

其他回答

已解决,请在这里找到详细信息,并使用下面的功能: 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!')

最简单的方法是:

用你的数据集在github上制作存储库 克隆您的存储库![GITHUB LINK REPO] 查找数据的位置(!ls命令) 用熊猫打开文件,就像用普通的jupyter笔记本一样。

您可以使用下面的函数。我假设您正在尝试上传一个数据帧类型的文件(.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()

这是在你没有改变谷歌合作目录的情况下,这是最简单的方法

下面是一种从谷歌驱动器导入文件到笔记本电脑的方法。

打开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")

我希望这对你们有帮助

我创建了一小段代码,可以以多种方式实现这一点。你可以

使用已经上传的文件(在重新启动内核时很有用) 使用来自Github的文件 手动上传文件

import os.path

filename = "your_file_name.csv"
if os.path.isfile(filename):
  print("File already exists. Will reuse the same ...")
else:
  use_github_data = False  # Set this to True if you want to download from Github
  if use_github_data:
    print("Loading fie from Github ...")
    # Change the link below to the file on the repo
    filename = "https://github.com/ngupta23/repo_name/blob/master/your_file_name.csv" 
  else:
    print("Please upload your file to Colab ...")
    from google.colab import files
    uploaded = files.upload()