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


当前回答

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

其他回答

步骤1-挂载您的谷歌驱动器到协作实验室

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

第2步-现在你会看到你的谷歌驱动器文件在左侧窗格(文件资源管理器)。右键单击需要导入的文件并选择çopy路径。 然后像往常一样在pandas中导入,使用这个复制的路径。

import pandas as pd
df=pd.read_csv('gdrive/My Drive/data.csv')

完成了!

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

到目前为止,我发现的最简单的解决方案,适用于中小型CSV文件是:

在gi.github.com上创建一个秘密要点,然后上传(或复制粘贴)你的文件。 单击Raw视图并复制原始文件URL。 在调用pandas.read_csv(URL)时,使用复制的URL作为文件地址

这对于逐行读取文本文件或二进制文件可能有效,也可能无效。

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

使用已经上传的文件(在重新启动内核时很有用) 使用来自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()

从谷歌。Colab导入驱动器

驱动器(' /内容/ drive’山)

进口熊猫作为pd dv = pd.read_csv(' /内容/传动/ MyDrive /戴安娜/卡索/ Data_Caso_Propuesto.csv”) dv.info ()