【问题标题】:Google colab: Read .xlsx file in from Github pandasGoogle colab:从 Github pandas 中读取 .xlsx 文件
【发布时间】:2021-09-27 08:25:59
【问题描述】:

来自 Google Colab,我正在尝试从 Github 存储库中的 xlsx 文件创建 df。 作为 url,我从 Github 获取永久链接,该 repo 是公开的,并且与 Colab 相关联

XLRDError:不支持的格式,或损坏的文件:预期的 BOF 记录;找到 b'\n\n\n\n\n\n

提前感谢您的帮助!

【问题讨论】:

  • 试试这个:xlrd.open_workbook(file_contents=your_bytes_string).

标签: python pandas google-colaboratory


【解决方案1】:

可能问题出在您使用的 URL 上。

您应该尝试执行此操作以查看 request.get 返回的内容。

url = "https://github.com/your-user-name/your-repo-name/blob/main/data/raw/your-file-name.xlsx"

import requests
from pprint import pprint

response = requests.get(url)
pprint(response.content)

这是一个 HTML 页面。这不是你想要的。

您可以采取一些措施来解决此问题。这篇中等帖子here 可能有用。

然而,一个简单的事情是使用如下示例的 URL:

https://raw.githubusercontent.com/your-username/name-of-the-repository/master/name-of-the-file.xlsx

我已经试过了,效果很好。

import requests
import pandas as pd

url = "https://raw.githubusercontent.com/your-username/name-of-the-repository/master/name-of-the-file.xlsx"

response = requests.get(url)

dest = 'local-file.xlsx'

with open(dest, 'wb') as file:
    file.write(response.content)

frame = pd.read_excel(dest)

frame.head()

结论:更改您的网址。

【讨论】:

    【解决方案2】:

    对于 Google Colab,您可以做的一件事是使用 wget 命令,就像这样。

    !wget "https://raw.githubusercontent.com/your-username/name-of-the-repository/master/name-of-the-file.xlsx"
    
    

    【讨论】:

    • 您介意删除此内容并将其编辑到您之前的答案中吗?不鼓励对同一问题发布多个答案。
    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 2020-09-17
    • 1970-01-01
    • 2021-06-04
    • 2023-02-03
    • 2021-07-01
    • 2021-11-05
    • 2021-07-02
    相关资源
    最近更新 更多