pandas - 从 S3 读取时 AWS Lambda 函数出错
问题描述
我正在尝试从 S3 存储桶中读取一个 excel 文件。这是我的 Lambda 函数代码,但在我使用pd.read_excel
.
我无法弄清楚这个问题,因为语法对我来说很好。读取数据有问题吗?请帮忙。
import json
import boto3
import pandas as pd
import io
def lambda_handler(event, context):
s3 = boto3.client("s3")
s3_resource = boto3.resource("s3")
if event:
s3_records = event["Records"][0]
bucket_name = str(s3_records["s3"]["bucket"]["name"])
file_name = str(s3_records["s3"]["object"]["key"])
file_obj = s3.get_object(Bucket=bucket_name, Key=file_name)
file_content = file_obj["Body"].read()
df = pd.read_excel(io.BytesIO(file_content, engine='xlrd')
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
这是日志:
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 23)
Traceback (most recent call last):
File "/var/task/lambda_function.py" Line 23
return {
解决方案
看来您在return
声明之前缺少右括号,应该是这样的:
df = pd.read_excel(io.BytesIO(file_content, engine='xlrd'))
而不是这个
df = pd.read_excel(io.BytesIO(file_content, engine='xlrd')
推荐阅读
- php - 用php在字符串中的-elements之间添加逗号
- python - 需要将具有计数列的 SQL 语句转换为 SQL Alchemy
- java - 随机整数的堆栈溢出
- laravel - Laravel @include 控制器数据
- r - 在R中将变量转换为字符串
- python - 如何使 Python 先输入所有内容,然后再输入首字母?
- python - 语法“cellule.utilisateurs.all”在 Django HTML 模板中有效,但在 Django TeX 模板中无效
- python - 如何获得上一层的形状并将其传递给下一层?
- excel-formula - 使用多个条件计算包含不等于许多事物的字符串的单元格 - Excel
- android - 在ionic3中可以使用指纹或面部ID登录吗?