Python:使用“ import csv”

时间:2019-09-09 07:30:25

标签: python csv amazon-s3

我当前正在尝试直接从AWS S3存储桶读取.csv。但是,我总是收到FileNotFoundError。在我实际上可以看到.csv文件的内容之后,很奇怪。

Traceback (most recent call last): File "<console>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: b',event_id,ds,yhat,yhat_lower,yhat_upper\n0,277,2019-09-04 07:14:08.051643,0.3054256311115928,0.29750667741533227,0.31441960581142636\n'

这是我的代码:

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,
)
obj = s3.get_object(Bucket=BUCKET_NAME, Key=FORECAST_DATA_OBJECT)
data = obj['Body'].read()

with open(data, newline='') as csvfile:
    spamreader = csv.reader(io.BytesIO(csvfile), delimiter=' ', quotechar='|')
    for row in spamreader:
        print(', '.join(row))

这是我的.csv文件的一些内容。理想情况下,我可以使用row ['event_id']作为字典来访问每一行。例如。访问 yhat 我可以只写row ['event_id'] ['yhat]。但是目前,这根本不起作用。

    event_id    ds  yhat    yhat_lower  yhat_upper
0   277 2019-09-04 7:14:08  0.3054256311    0.2975066774    0.3144196058
0   178 2019-09-28  0.3454256311    0.2275066774    0.3944196058

2 个答案:

答案 0 :(得分:3)

只需摆脱with open(data, newline='') as csvfile:
因为open需要本地文件系统上的文件名。
您应将data直接传递给io.BytesIO

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,
)
obj = s3.get_object(Bucket=BUCKET_NAME, Key=FORECAST_DATA_OBJECT)
data = obj['Body'].read().decode('utf-8')
spamreader = csv.reader(io.StringIO(data), delimiter=' ', quotechar='|')
for row in spamreader:
    print(', '.join(row))

编辑:显然,csv.reader需要字符串而不是字节, 因此您需要解码响应并将数据包装在is.StringIO中。

答案 1 :(得分:0)

使用:

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