google-colaboratory - 文件 b'content/train.csv' 不存在 Google Colab
问题描述
第一次使用 Google Colab。我使用了 Kaggle API,并将数据加载到 Google Colab 中,但我似乎无法通过 pandas 打开它。我右键单击文件并复制路径。然后我运行以下代码:
import pandas as pd
train = pd.read_csv("content/train.csv")
test = pd.read_csv('content/test.csv')
我得到的错误代码:
FileNotFoundError: File b'content/train.csv' does not exist
这是我为导致此错误所做的一切的代码:
!pip install kaggle
from google.colab import files
files.upload() #Uploaded my kaggle.json file
!pip install -q kaggle
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!kaggle competitions download -c microsoft-malware-prediction
#Unzip the files:
!7z x train.csv.zip
!7z x sample_submission.csv.zip
!7z x test.csv.zip
#remove the zipped data
!rm train.csv.zip
!rm sample_submission.csv.zip
!rm test.csv.zip
import pandas as pd
train = pd.read_csv("content/train.csv")
test = pd.read_csv('content/test.csv')
print('read')
任何帮助都会很棒!
解决方案
它也发生在我身上,但我能够通过使用新语法读取 .csv 文件来解决:
- 在上面的代码块中输入这个(第一个或第二个)
!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
#Authenticate and create the PyDrive client
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
然后这样做:
link = 'link_to_file_in drive'
fluff, id = link.split('=')
downloaded = drive.CreateFile({'id':id})
downloaded.GetContentFile('name_of_file.csv')
df = pd.read_csv("name_of_file.csv")