代码之家  ›  专栏  ›  技术社区  ›  dd_rookie

从colaboratory中的驱动器加载xlsx文件

  •  10
  • dd_rookie  · 技术社区  · 8 年前

    如何将MS excel(.xlsx)文件从google drive导入colaboratory?

    excel_file = drive.CreateFile({'id':'some id'})
    

    确实有效( drive 是一个 pydrive.drive.GoogleDrive 对象)。但是

    print excel_file.FetchContent()
    

    返回None。和

    excel_file.content()
    

    抛出:

    TypeErrorTraceback(上次调用) 在() ----&燃气轮机;1个excel_文件。内容()

    类型错误:'\u io。BytesIO对象不可调用

    我的意图是(给定一些有效的文件“id”)将其作为io对象导入,该对象可以被pandas读取 read_excel() ,最后从中获取熊猫数据帧。

    5 回复  |  直到 8 年前
        1
  •  10
  •   Bob Smith    8 年前

    你会想要使用 excel_file.GetContentFile 在本地保存文件。然后,你可以用熊猫 read_excel 方法在你之后 !pip install -q xlrd .

    下面是一个完整的示例: https://colab.research.google.com/notebook#fileId=1SU176zTQvhflodEzuiacNrzxFQ6fWeWC

    我做的更详细:

    我创建了一个新的 spreadsheet in sheets 作为导出。xlsx文件。

    接下来,我将其导出为。xlsx文件并再次上传到驱动器。URL为: https://drive.google.com/open?id=1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM

    注意文件ID。在我的情况下,它是 1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM .

    然后,在Colab中,我调整了 Drive download snippet 下载文件。关键位包括:

    file_id = '1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM'
    downloaded = drive.CreateFile({'id': file_id})
    downloaded.GetContentFile('exported.xlsx')
    

    最后,要创建熊猫数据帧:

    !pip install -q xlrd
    import pandas as pd
    df = pd.read_excel('exported.xlsx')
    df
    

    这个 !pip install... line安装xlrd库,用于读取Excel文件。

        2
  •  7
  •   Zilong Z    5 年前

    也许是一种更简单的方法:

    #To read/write data from Google Drive:
    #Reference: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveAÃ¥
    from google.colab import drive
    drive.mount('/content/drive')
    
    df = pd.read_excel('/content/drive/My Drive/folder_name/file_name.xlsx')
    
    # #When done, 
    # drive.flush_and_unmount()
    # print('All changes made in this colab session should now be visible in Drive.')
    
    
        3
  •  3
  •   neosergio    5 年前

    首先,我导入 io , 熊猫 文件夹 谷歌。colab公司

    import io
    import pandas as pd
    from google.colab import files
    

    然后我使用上传小部件上传文件

    uploaded = files.upload()
    

    您将看到类似的内容(单击“选择文件”并上载xlsx文件): enter image description here

    假设文件的名称是my_电子表格。xlsx,因此您需要在以下行中使用它:

    df = pd.read_excel(io.BytesIO(uploaded.get('my_spreadsheet.xlsx')))
    

    就这样,现在你有了 测向 数据帧。但是,如果您有多张工作表,可以将代码更改为:

    首先,将io调用移动到另一个变量

    xlsx_file = io.BytesIO(uploaded.get('my_spreadsheet.xlsx'))
    

    然后,使用新变量指定图纸名称,如下所示:

    df_first_sheet = pd.read_excel(xlsx_file, 'My First Sheet')
    df_second_sheet = pd.read_excel(xlsx_file, 'My Second Sheet')
    
        4
  •  1
  •   willhyper    4 年前
    import pandas as pd
    
    xlsx_link = 'https://docs.google.com/spreadsheets/d/1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM/export'
    df = pd.read_excel(xlsx_link)
    

    如果xlsx托管在Google drive上,一旦共享,任何人都可以使用链接访问它,无论是否有Google帐户。 google.colab.drive google.colab.files 不需要依赖项

        5
  •  0
  •   Vinicius Raphael    3 年前

    到目前为止,我找到了最简单的方法。

    非常类似于我们在桌面上所做的。

    考虑到您已将文件上载到Google Drive文件夹:

    • 在左侧栏上单击文件(在{x}下方)
    • 选择Mount Driver(安装驱动程序)>驱动(>);文件夹(>);文件(左键单击并复制路径)

    之后,只需转到代码并通过路径即可

    pd.read_excel('/content/drive/MyDrive/Colab Notebooks/token_rating.xlsx')