想试试谷歌提供的免费计算资源,试试用colab,但是数据从谷歌云盘与colab不知道如何导入和导出,花费一个上午的时间搞出来了。
一、首先创建一个随便叫什么的文件夹。
image.png
二、上传数据csv,获取文件id
image.png
image.png
三、新建ipynb文件
image.png
四、读取csv文件
# Code to read csv file into colaboratory:
!pip install -U -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)
#2. Get the file #在此处吧你的文件id改进去
downloaded = drive.CreateFile({'id':'yourfileID'}) # replace the id with id of file you want to access
#输入你的文件名字
downloaded.GetContentFile('sample_submission (3).csv')
#3. Read file as panda dataframe
import pandas as pd
xyz = pd.read_csv('sample_submission (3).csv')
然后会出现这个界面
image.png
点链接进去,一路允许,然后复制你得到的code,复制进去回车。
ok,文件已经导入进去了。
image.png
五、写入到谷歌云盘
xyz.to_csv('over.csv')
#保存文件
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# Create & upload a text file.
#你想要导出的文件的名字
uploaded = drive.CreateFile({'title': 'OK.csv'})
#改为之前生成文件的名字
uploaded.SetContentFile('over.csv')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))
文件就出现在了google云盘里了,OK.csv
image.png