Python:如何从 AWS S3 读取和加载 excel 文件? [英] Python: How to read and load an excel file from AWS S3?

查看:30
本文介绍了Python:如何从 AWS S3 读取和加载 excel 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将一个 excel 文件上传到 AWS S3 存储桶,现在我想在 python 中读取它.任何帮助,将不胜感激.这是我迄今为止取得的成就,

I have uploaded an excel file to AWS S3 bucket and now I want to read it in python. Any help would be appreciated. Here is what I have achieved so far,

import boto3
import os

aws_id = 'aws_id'
aws_secret = 'aws_secret_key'

client = boto3.client('s3', aws_access_key_id=aws_id, aws_secret_access_key=aws_secret)
bucket_name = 'my_bucket'
object_key = 'my_excel_file.xlsm'
object_file = client.get_object(Bucket=bucket_name, Key=object_key)
body = object_file['Body']
data = body.read()

接下来我需要做什么才能读取这些数据并对其进行处理?

What do I need to do next in order to read this data and work on it?

推荐答案

在这上面花了很多时间,这就是我如何让它工作的,

Spent quite some time on it and here's how I got it working,

import boto3
import io
import pandas as pd
import json

aws_id = ''
aws_secret = ''
bucket_name = ''
object_key = ''

s3 = boto3.client('s3', aws_access_key_id=aws_id, aws_secret_access_key=aws_secret)
obj = s3.get_object(Bucket=bucket_name, Key=object_key)
data = obj['Body'].read()
df = pd.read_excel(io.BytesIO(data), encoding='utf-8')

这篇关于Python:如何从 AWS S3 读取和加载 excel 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆