Python:从S3存储桶中读取不带熊猫的CSV文件

时间:2019-09-07 18:16:46

标签: python amazon-web-services amazon-s3

我的目标是访问S3存储桶中的csv文件。该文件包含以下列:event_id, ds, yhat, yhat_lower, yhat_upper。我发现以下示例here

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))

但是,这里没有解决的是如何将其直接应用于S3存储桶。这就是我当前尝试访问文件的代码:

BUCKET_NAME = 'fbprophet'
FORECAST_DATA_OBJECT = 'forecast.csv'
s3 = boto3.client(
    's3',
    aws_access_key_id=settings.ML_AWS_ACCESS_KEY_ID,
    aws_secret_access_key=settings.ML_AWS_SECRET_ACCESS_KEY,
)
# 's3' is a key word. create connection to S3 using default config and all buckets within S3
csv_obj = s3.get_object(Bucket=BUCKET_NAME, Key=FORECAST_DATA_OBJECT)

更新:

obj = s3.get_object(Bucket=BUCKET_NAME, Key=FORECAST_DATA_OBJECT)
data = obj['Body'].read()

spamreader = csv.reader(data, delimiter=' ', quotechar='|')
for row in spamreader:
    print(', '.join(row))

0 个答案:

没有答案