[google-sheets] Google Colaboratory로 데이터 가져 오기

비공개 데이터를 Google Colaboratory 노트북으로 가져 오는 일반적인 방법은 무엇입니까? 비공개 Google 시트를 가져올 수 있습니까? 시스템 파일에서 읽을 수 없습니다. 소개 문서는 BigQuery 사용 에 대한 가이드와 연결되어 있지만 조금은 …



답변

https://colab.research.google.com/notebooks/io.ipynb : 로컬 파일 업로드 / 다운로드 및 드라이브 및 시트와의 통합을 보여주는 공식 노트북

파일을 공유하는 가장 간단한 방법은 Google 드라이브를 마운트하는 것입니다.

이렇게하려면 코드 셀에서 다음을 실행하십시오.

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

드라이브에 액세스하려면 “Google 파일 스트림”허용 링크를 방문하라는 메시지가 표시됩니다. 그런 다음 Colab의 노트북에 입력해야하는 긴 영숫자 인증 코드가 표시됩니다.

그 후 드라이브 파일이 마운트되고 측면 패널의 파일 브라우저로 파일을 찾아 볼 수 있습니다.

여기에 이미지 설명을 입력하십시오

다음은 전체 예제 노트북입니다.


답변

업로드

from google.colab import files
files.upload()

다운로드

files.download('filename')

디렉토리 목록

files.os.listdir()


답변

googledrive에서 데이터를 가져 오는 간단한 방법-이렇게하면 사람들이 시간을 절약 할 수 있습니다 (Google이이 단계를 명시 적으로 나열하지 않는 이유를 모릅니다).

PyDrive 설치 및 인증

     !pip install -U -q PyDrive ## you will have install for every colab session

     from pydrive.auth import GoogleAuth
     from pydrive.drive import GoogleDrive
     from google.colab import auth
     from oauth2client.client import GoogleCredentials

     # 1. Authenticate and create the PyDrive client.
     auth.authenticate_user()
     gauth = GoogleAuth()
     gauth.credentials = GoogleCredentials.get_application_default()
     drive = GoogleDrive(gauth)

업로드

로컬 드라이브에서 데이터를 업로드해야하는 경우 :

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

실행하면 파일 선택 버튼이 표시됩니다-업로드 파일을 찾으십시오-열기를 클릭하십시오

업로드하면 다음이 표시됩니다.

    sample_file.json(text/plain) - 11733 bytes, last modified: x/xx/2018 - %100 done
    User uploaded file "sample_file.json" with length 11733 bytes

노트북 용 파일 작성

데이터 파일이 이미 gdrive에 있으면이 단계로 건너 뛸 수 있습니다.

이제 구글 드라이브에 있습니다. Google 드라이브에서 파일을 찾아 마우스 오른쪽 버튼으로 클릭하십시오. ‘공유 가능한 링크’를 클릭하십시오. 다음과 같은 창이 나타납니다.

    https://drive.google.com/open?id=29PGh8XCts3mlMP6zRphvnIcbv27boawn

복사- ’29PGh8XCts3mlMP6zRphvnIcbv27boawn’-파일 ID입니다.

노트북에서 :

    json_import = drive.CreateFile({'id':'29PGh8XCts3mlMP6zRphvnIcbv27boawn'})

    json_import.GetContentFile('sample.json') - 'sample.json' is the file name that will be accessible in the notebook.

노트북으로 데이터 가져 오기

노트북에 업로드 한 데이터를 가져 오려면 (이 예제에서 json 파일-로드 방법은 파일 / 데이터 유형 (.txt, .csv 등)에 따라 다름) :

    sample_uploaded_data = json.load(open('sample.json'))

이제 데이터가 있는지 확인하기 위해 인쇄 할 수 있습니다.

    print(sample_uploaded_data)


답변

1 단계-Google 드라이브를 공동 작업에 마운트

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

2 단계 : 이제 왼쪽 창 (파일 탐색기)에 Google 드라이브 파일이 표시됩니다. 가져와야 할 파일을 마우스 오른쪽 버튼으로 클릭하고 çopy path를 선택하십시오. 그런 다음이 복사 된 경로를 사용하여 팬더에서 평소와 같이 가져옵니다.

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

끝난!


답변

내가 만든 가장 간단한 방법은 다음과 같습니다.

  1. 데이터 세트로 github에 리포지토리 만들기
  2. !로 저장소를 복제하십시오. 자식 복제-재귀 [GITHUB LINK REPO]
  3. 데이터가 어디에 있는지 확인하십시오 (! ls 명령)
  4. 일반적인 jupyter 노트북에서와 마찬가지로 팬더가있는 파일을 엽니 다.

답변

이를 통해 Google 드라이브를 통해 파일을 업로드 할 수 있습니다.

아래 코드를 실행하십시오 (이전에 어딘가 찾았지만 소스를 다시 찾을 수 없습니다-그것을 작성한 사람에게 크레딧!) :

!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에 로그인하라는 메시지가 표시됩니다. 그런 다음 Google 드라이브에 대한 액세스 권한을 요청하는 다른 메시지가 나타납니다.

그런 다음 ‘drive’라는 디렉토리를 만들고 Google 드라이브를 해당 디렉토리에 연결합니다.

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

당신이 할 경우 !ls현재 디렉토리 드라이브가 될 것이며, 당신이 할 경우 !ls drive당신은 당신의 Google 드라이브의 모든 내용을 볼 수 있습니다.

예를 들어 Google 드라이브 abc.txt에있는 폴더에 파일을 저장 ColabNotebooks하면 경로를 통해 파일에 액세스 할 수 있습니다.drive/ColabNotebooks/abc.txt


답변

협업의 왼쪽 막대에는 “파일”이라는 섹션이 있습니다. 거기에 파일을 업로드 하고이 경로를 사용하십시오

"/content/YourFileName.extension"

전의: pd.read_csv('/content/Forbes2015.csv');