【问题标题】:read json file with Python from S3 into sagemaker notebook使用 Python 从 S3 将 json 文件读取到 sagemaker 笔记本中
【发布时间】:2019-04-18 02:44:51
【问题描述】:

我想将一个 json 文件从 S3 读入一个 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] 没有这样的文件或目录

【问题讨论】:

  • 尝试使用array = json.loads(file.read())

标签: python json amazon-s3 amazon-sagemaker


【解决方案1】:

Json.load() 需要本地文件系统路径“/...”,而不是“s3://”URI。
在这里查看答案:https://stackoverflow.com/a/47121263

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2019-05-15
    • 2017-04-21
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多