本文介绍了从S3读取Excel-AttributeError:"StreamingBody"对象没有属性"seek"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,该脚本从S3读取一个excel文件,但在AWS Batch中触发该文件时出现错误.该代码可以在另一个Ubuntu机器上正常工作.

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'

下面是我读取Excel的代码部分

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)

非常感谢您的帮助.

推荐答案

似乎read_excel更改了传入的类似于文件"对象的要求,并且此对象现在必须具有seek方法.我通过将 pd.read_excel(obj ['Body'])更改为 pd.read_excel(io.BytesIO(file_obj ['Body'].read()))

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

这篇关于从S3读取Excel-AttributeError:"StreamingBody"对象没有属性"seek"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 14:41