【问题标题】:Import Excel file to pandas from Github repository从 Github 存储库将 Excel 文件导入 pandas
【发布时间】:2021-03-25 18:45:22
【问题描述】:

我努力使用以下源代码将我的私人 Github 存储库中的 Excel 文件导出到 Pandas 数据框:

username= 'xxx'
token = 'yyyy'
github_session = requests.Session()
github_session.auth = (username, token)

url = 'correct path to raw file'
export = requests.get(url).content

df = pd.read_excel(io.StringIO(export.decode('utf-8')))

我在运行上述块的最后一行时遇到了错误:

TypeError: unsupported operand type(s) for <<: 'str' and 'int'

来自 Github 存储库的 Excel 原始数据文件结合了数字和字符串记录。

【问题讨论】:

    标签: python pandas github import


    【解决方案1】:

    Excel 是一种二进制文件格式。请改用io.BytesIO(export)

    【讨论】:

    • 感谢您的评论。 pd.read_excel(io.BytesIO(export.decode('utf-8'))) 返回类型错误如下:TypeError: a bytes-like object is required, not 'str'pd.read_excel(io.BytesIO(export)) 返回以下错误:xlrd.biffh.XLRDError: Unsupported format ,或损坏的文件:预期的 BOF 记录;找到 b'\n\n\n\n\n
    • 不要使用.decode; decode 将其转换为字符串格式
    猜你喜欢
    • 2020-01-21
    • 2017-04-22
    • 2018-04-04
    • 1970-01-01
    • 2013-11-12
    • 2018-01-06
    • 2020-09-03
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多