---
# System prepended metadata

title: '【Colab 讀取資料 : Github、Google Drive、本地端】'

---

# **【Colab讀取資料 : Github、Google Drive、本地端】** 

:::info
- Github
    - 直接讀取
    - 存成臨時文件再讀取
- Google Drive
- 本地端
:::


### Github

直接讀取
```=
import pandas as pd

url = 'https://raw.githubusercontent.com/06Cata/Kaggle_Titanic/main/raw_data/train.csv'

df = pd.read_csv(url)

df.head()
```
![螢幕擷取畫面 2024-06-23 173348](https://hackmd.io/_uploads/r1xz3wS8C.png)

存成臨時文件再讀取

```=
import pandas as pd
import tempfile
import requests

url = 'https://raw.githubusercontent.com/06Cata/Kaggle_Titanic/main/raw_data/train.csv'
response = requests.get(url)

# 數據保存到臨時文件中
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(response.content)
temp_file_path = temp_file.name

df2 = pd.read_csv(train_file_path)
df2
```
![螢幕擷取畫面 2024-06-23 173348](https://hackmd.io/_uploads/r1xz3wS8C.png)


<br/>


### Google Drive


```=
!pip install -U -q PyDrive

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

# 認證，這裡會跳出google登入頁面，選自己的帳號按繼續
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# google drive 文件夾連結
link = 'https://drive.google.com/file/d/1tPJeYAHyFGhHmdDTSzvuiBSXbVH3uWgM/view?usp=sharing'

# 提取文件夾ID
file_id = link.split('/d/')[1].split('/')[0]

# download
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('train.csv')

df_drive = pd.read_csv('train.csv')
df_drive
```
![螢幕擷取畫面 2024-06-23 180521](https://hackmd.io/_uploads/r19tmdHLA.png)



<br/>

### 本地端
用上傳文件的方式
會先跳出【選擇檔案】，直接選本地端的檔案
```=
from google.colab import files
import pandas as pd
import io

uploaded = files.upload()

file_name = 'train.csv'
df3 = pd.read_csv(io.BytesIO(uploaded[file_name]))

df3.head()
```
![螢幕擷取畫面 2024-06-23 174454](https://hackmd.io/_uploads/H1f20vB8R.png)

