问题描述
如何将Google驱动器中的MS-excel(.xlsx)文件导入colaboratory?
How can I import MS-excel(.xlsx) file from google drive into colaboratory?
excel_file = drive.CreateFile({'id':'some id'})
可以工作(drive
是pydrive.drive.GoogleDrive
对象).但是
does work(drive
is a pydrive.drive.GoogleDrive
object). But,
print excel_file.FetchContent()
不返回任何内容.还有
excel_file.content()
抛出:
TypeError:"_ io.BytesIO"对象不可调用
TypeError: '_io.BytesIO' object is not callable
我的意图是(给出一些有效的文件'id')将其导入为io对象,熊猫read_excel()
可以读取该对象,并最终从中获取熊猫数据框.
My intent is (given some valid file 'id') to import it as an io object, which could be read by pandas read_excel()
, and finally get a pandas dataframe out of it.
推荐答案
您将要使用excel_file.GetContentFile
在本地保存文件.然后,您可以在!pip install -q xlrd
之后使用Pandas read_excel
方法.
You'll want to use excel_file.GetContentFile
to save the file locally. Then, you can use the Pandas read_excel
method after you !pip install -q xlrd
.
这是一个完整的例子: https://colab.research.google.com/notebook#fileId=1SU176zTQvhflodEzuiacNrzxFQ6fWeWC
Here's a full example:https://colab.research.google.com/notebook#fileId=1SU176zTQvhflodEzuiacNrzxFQ6fWeWC
我更详细地做了什么:
我创建了一个新的表格中的电子表格以便导出作为.xlsx文件.
I created a new spreadsheet in sheets to be exported as an .xlsx file.
接下来,我将其导出为.xlsx文件,然后再次上传到云端硬盘.网址是: https://drive.google.com/open?id=1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM
Next, I exported it as an .xlsx file and uploaded again to Drive. The URL is:https://drive.google.com/open?id=1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM
记下文件ID.就我而言,是1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM
.
Note the file ID. In my case it's 1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM
.
然后,在Colab中,我调整了驱动器下载代码段以下载文件.关键位是:
Then, in Colab, I tweaked the Drive download snippet to download the file. The key bits are:
file_id = '1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM'
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('exported.xlsx')
最后,创建一个熊猫数据框:
Finally, to create a Pandas DataFrame:
!pip install -q xlrd
import pandas as pd
df = pd.read_excel('exported.xlsx')
df
!pip install...
行安装了xlrd库,该库是读取Excel文件所必需的.
The !pip install...
line installs the xlrd library, which is needed to read Excel files.
这篇关于从驱动器中加载xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!