读取公共json fi时发生Python FileNotFoundError

2024-04-26 01:22:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个公共可见的JSON文件,托管在s3上。我可以通过单击s3对象的公共url直接访问该文件,并在浏览器中查看JSON。互联网上的任何人都可以轻松地查看该文件。你知道吗

但是下面的代码是用Python运行的(使用连接到API触发器的Lambda),我得到[Errno 2] No such file or directory:作为errorMessage,而FileNotFoundError作为errorType。你知道吗

def readLeetDictionary(self):
        jsonfile = 'https://s3.amazonaws.com/url/to/public/facing/file/data.json'
        with open(jsonfile, 'r') as data_file:
            self.data = json.load(data_file)

我错过了什么?由于该文件是一个可公开查看的JSON文件,我假设我不会被迫使用boto3库并正式地与该文件握手以读取该文件(例如使用object = s3.get_object(Bucket=bucket_names,Key=object_name))-我会吗?你知道吗


Tags: 文件对象代码selfapijsonurldata
1条回答
网友
1楼 · 发布于 2024-04-26 01:22:57

您需要的conde应该是:

import urllib, json
def readLeetDictionary():
      jsonfile = 'https://s3.amazonaws.com/url/to/public/facing/file/data.json'
      response = urllib.urlopen(jsonfile)
      data = json.loads(response.read())
      print data

请随时进一步询问或解释,如果这不适合你。你知道吗

相关问题 更多 >