首页 > 解决方案 > Pd.read_excel 错误 - AttributeError: 'StreamingBody' 对象没有属性 'seek

问题描述

我正在尝试将存储在我的项目数据资产中的 excel 文件读入我在 ibm watson studio 上的笔记本中,并且正在检索以下错误:

AttributeError:“StreamingBody”对象没有“seek”属性

这是我从包含的选项菜单中使用的代码(api key id 被故意编辑掉):

import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

# @hidden_cell
# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.
# You might want to remove those credentials before you share the notebook.
client_7de401550a6447db83336f61dc6f7a36 = ibm_boto3.client(service_name='s3',
    ibm_api_key_id='....',
    ibm_auth_endpoint="https://iam.cloud.ibm.com/oidc/token",
    config=Config(signature_version='oauth'),
    endpoint_url='https://s3-api.us-geo.objectstorage.service.networklayer.com')

body = client_7de401550a6447db83336f61dc6f7a36.get_object(Bucket='courseracapstone-donotdelete-pr-gulobge2viwrrq',Key='business-licences.xlsx')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

df_data_0 = pd.read_excel(body)
df_data_0.head()

标签: pythonpandas

解决方案


看起来 read_excel 已经改变了对传入的“类似文件”对象的要求,并且这个对象现在必须有一个 seek 方法。我通过更改pd.read_excel(obj['Body'])为解决了这个问题pd.read_excel(io.BytesIO(file_obj['Body'].read()))

参考:-从 S3 读取 Excel - AttributeError: 'StreamingBody' 对象没有属性 'seek'


推荐阅读