无法解决“json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig) error”。谁能帮忙?

瑞安

我正在尝试访问 S3 存储桶中的对象,这是一个 JSON 文件,并试图在 Visual Studio 代码中打开它。但我收到错误“json.decoder.JSONDecodeError:意外的 UTF-8 BOM(使用 utf-8-sig 解码)”

import boto3
import json
import requests
boto3.setup_default_session(profile_name='rli-dev', region_name='us-west-2')
s3 = boto3.resource('s3')
content_object = s3.Object('bsdev-data-validation','awsnightlyendtoend_bsdev_2018-10-24T11:53:45Z/validate_adwactivityfact/job-details.json')

file_content = content_object.get()['Body'].read().decode()
json_content = json.loads(file_content)
print(json_content)
buran

的默认编码decode()utf-8. 回溯指定文件的编码,utf-8 BOM因此您需要传递utf-8-sigdecode(),例如

file_content = content_object.get()['Body'].read().decode('utf-8-sig')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章