在 Python 中,我有一个函数,其中包含 try 和 except 块,假设如果该 try 块中有 3 到 5 个错误,我需要捕获每个错误并将其存储为字典
我创建了字典来存储在其中,但只有一个错误正在存储,我需要存储每个错误
import io
import pyarrow.parquet as pq
import pandas as pd
import boto3
# AWS credentials and session token
aws_access_key_id = #####
aws_secret_access_key = #####
aws_session_token = #######
region_name = 'us-east-1' # Specify your AWS region here
# Initialize session and clients
session = boto3.session.Session(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
region_name=region_name
)
s3 = session.client('s3')
sns = session.client('sns')
Topic_Arn = "arn:aws:sns:us-east-1:614414489128:Capturing_Every_Errors"
def read_parquet_from_s3(bucket_name, file_key, s3_client):
error_messages = [] # Initialize an empty list to store error messages
try:
# Read the Parquet file from S3
s3_object = s3_client.get_object(Bucket=bucket_name, Key=file_key)
parquet_content = s3_object['Body'].read()
a = ajay
# Load the Parquet content into a pyarrow table
parquet_table = pq.read_table(io.BytesIO(parquet_content))
b = xyz
# Convert to a Pandas DataFrame
df = parquet_table.to_pandas()
return df
except Exception as e:
error_messages.append(str(e)) # Append error message to list
print(f"An error occurred: {e}")
# Send SNS notification with all accumulated errors
if error_messages:
error_message = "\n".join(error_messages)
send_sns_notification(error_message)
return None
def send_sns_notification(message):
try:
sns.publish(
TopicArn=Topic_Arn,
Message=message,
Subject="Error Notification from S3 Parquet Reader"
)
print("SNS Sent Successful")
except Exception as (e):
print(f"Failed to send SNS notification: {e}")
# Example usage
s3_bucket_name = ########
s3_input_key = ########
# Read the Parquet file from S3 and get the DataFrame
df = read_parquet_from_s3(s3_bucket_name, s3_input_key, s3)
if df is not None:
print(df)
else:
print("Failed to read the Parquet file.")`
对于此代码,我需要获取应存储在错误字典中的多个错误
需要在单个 try 块中捕获多个异常
简短的回答:这是不可能的,因为在第一次遇到错误后会退出单个 try 块(按设计)。
使用多个或嵌套的 try/ except 块来达到您想要的效果。