简体   繁体   English

Python:使用“import csv”从 S3 存储桶中读取 CSV

[英]Python: Read CSV from S3 bucket with `import csv`

I am currently trying to read a .csv directly from an AWS S3 bucket.我目前正在尝试直接从 AWS S3 存储桶读取.csv However, I am always receiving a FileNotFoundError .但是,我总是收到FileNotFoundError Weirdly after I can actually see the content of the .csv file.奇怪的是,我实际上可以看到 .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'

Here my code:这是我的代码:

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))

And here some content of my .csv file.这里是我的 .csv 文件的一些内容。 I ideally I could access each row as a dictionary with row['event_id'].理想情况下,我可以将每一行作为带有 row['event_id'] 的字典访问。 Eg to access yhat I could just write row['event_id']['yhat].例如,要访问yhat,我可以只写 row['event_id']['yhat]。 But currently, that's not how it works at all.但目前,这根本不是它的工作方式。

    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

Just get rid of with open(data, newline='') as csvfile:只需将with open(data, newline='') as csvfile:
because open expects a name of a file on your local filesystem.因为open需要本地文件系统上的文件名。
You should pass data to io.BytesIO directly.您应该直接将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))

Edit : Apparently csv.reader expects strings, not bytes, so you need to decode the response and wrap data in is.StringIO instead.编辑:显然csv.reader需要字符串,而不是字节,因此您需要解码响应并将数据包装在is.StringIO

Use:用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 如何读取从 S3 存储桶检索的 CSV 文件? - Python - How to read CSV file retrieved from S3 bucket? 无法从s3存储桶读取大型csv文件到python - unable to read large csv file from s3 bucket to python AWS Lambda:使用Python从s3存储桶中读取csv文件尺寸,而无需使用Pandas或CSV包 - AWS Lambda: read csv file dimensions from an s3 bucket with Python without using Pandas or CSV package 从 S3 存储桶中读取大量 CSV 文件 - Read large number of CSV files from S3 bucket 如何从 AWS Lambda 中的 s3 存储桶读取 csv 文件? - How to read csv file from s3 bucket in AWS Lambda? 从 S3 存储桶中的 CSV 文件中读取数据并将其存储在 python 的字典中 - Read data from a CSV file in S3 bucket and store it in a dictionary in python 如何使用 Python 中的 Pandas 从 s3 存储桶中读取 csv 文件 - How to read a csv file from an s3 bucket using Pandas in Python csv 未填充到 s3 存储桶中 - csv not populating in s3 bucket 如何从 S3 存储桶中读取 CSV 文件,对其应用某些 if 语句,并编写新的更新的 CSV 文件并将其放入 S3 存储桶? - How can I read from a CSV file from an S3 bucket, apply certain if-statements to it, and write a new updated CSV file and place it in the S3 bucket? 使用python2.7从Amazon s3读取csv - Read csv from Amazon s3 using python2.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM