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

狂风中的少年 提交于 2021-02-19 04:19:42

问题


I have a python script which reads an excel file from S3 but getting an error when it's triggered in AWS Batch. The code works fine on another Ubuntu box.

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

Section of my code to read the excel is below

import boto3
import pandas as pd    
session = boto3.Session(aws_access_key_id = config.access_key_id, aws_secret_access_key = config.secret_access_key)
client = session.client('s3') 
obj = client.get_object(Bucket = s3_bucket, Key = s3_file)    
df = pd.read_excel(obj['Body'],sheet_name=sheet_name, skiprows=1)

Any help is much appreciated.


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




回答2:


Changing pandas version may do the job too.

pip install --upgrade pandas==1.0.1


来源:https://stackoverflow.com/questions/57815246/read-excel-from-s3-attributeerror-streamingbody-object-has-no-attribute-se

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!