2018년 5월 27일 일요일

Colaboratory - load file from Google Drive



http://jusths.tistory.com/6    에서 퍼옴



상황

-  데이터파일이 나의 Google Drive 있고
이것을 Colaboratory 가져와서 pandas  신나게 요리하고 싶다.

링크

공식예제https://goo.gl/XHVLnF
- Stackoverflow 링크https://goo.gl/kqBqLD

 다양한 해법이 있겠으나 공식예제의 PyDrive  사용하겠음

해보자

1. Google Drive  위치한 파일의 id  알아내자

원하는 파일을 우클릭  "Get shareable link 선택
여기서 링크의 id 부분을 저장해둔다

1bGTPirOnwDlzRABCEDWtGkQ2a1bAaMWY 부분이 id 이다.

2. Colaboratory 초기 작업

PyDrive  설치하고구글 인증을 하는 부분이니 그냥 실행하면 된다.

!pip install --q PyDrive
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)
cs


3. Load 하기

1) file_id  Google Drive 에서 얻어낸 
2) CreateFile()  가져온 다음
3) GetContentFile()  명시한 파일명으로 Colaboratory  저장한다원래 파일명이 아니라도 된다 
  file_id = '1bGTPirOnwDlzRABCEDWtGkQ2a1bAaMWY'
  downloaded = drive.CreateFile({'id': file_id})
  downloaded.GetContentFile('schema.csv')
cs

4. pandas dataframe 으로 가져오기

1) !ls -al 명령으로 Colaboratory  현재폴더에 schma.csv 파일이 들어와 있는 것이 보인다.
2) pandas  읽어들이고 일부를 출력해보니  가져왔다.




















댓글 없음:

댓글 쓰기