1

I am trying to read an excel file that is stored in my project data assets into my notebook on ibm watson studio and am retrieving the following error:

AttributeError: 'StreamingBody' object has no attribute 'seek'

Here is the code I used from the included option menu (api key id was edited out on purpose):

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()

opk2009
  • 11
  • 4

3 Answers3

1

It seems like read_excel has changed the requirements for the "file like" object passed in, and this object now has to have a seek method. I solved this by changing pd.read_excel(obj['Body']) to pd.read_excel(io.BytesIO(file_obj['Body'].read()))

Reference:- Read Excel from S3 - AttributeError: 'StreamingBody' object has no attribute 'seek'

Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
0

Try to replace the last two lines, with these:

import io
df_data_0 = pd.read_excel(io.BytesIO(body.read()))
df_data_0.head()
Rishabh Kumar
  • 2,342
  • 3
  • 13
  • 23
0

For those taking the IBM certification course. Change the line to pd.read_excel(Body.read()). Same answer as above but the Watson studio does not have an object called io. So it will error if you directly copy-paste the top answer.

Again use pd.read_excel(Body.read())

StupidWolf
  • 45,075
  • 17
  • 40
  • 72