从S3读取json文件到sagemaker notebook

问题描述 投票:0回答:1

我想从S3读取一个json文件到sagemaker笔记本中。

我可以使用此代码使用pandas执行此操作,这可以正常工作:

import json
import pandas as pd
import boto3


prefix_source = 'folder'

s3 = boto3.resource('s3')
my_bucket_source = s3.Bucket('bucket_source')

for obj in my_bucket_source.objects.filter(Prefix=prefix_source):
        data_location = 's3://{}/{}'.format(obj.bucket_name, obj.key)
        data = pd.read_json(data_location, lines = True )
        display(data.head())

但是我不想使用pandas,我想使用Python

我试过这段代码

for obj in my_bucket_source.objects.filter(Prefix=prefix_source):
        data_location = 's3://{}/{}'.format(obj.bucket_name, obj.key)
        with open(data_location, 'r') as f:
            array = json.load(f)
            display(array) 

我收到了这个错误:

IOError:[Errno 2]没有这样的文件或目录

python json amazon-s3 amazon-sagemaker
1个回答
2
投票

Json.load()期望本地文件系统路径“/ ...”,而不是“s3://”URI。 请在此处查看答案:https://stackoverflow.com/a/47121263

© www.soinside.com 2019 - 2024. All rights reserved.