首页 > 解决方案 > 从 s3 存储桶元文件中提取值

问题描述

我使用从 S3 Bucket 读取文件obj.get()['Body'].read()并返回

b'[{ "version": "v1", "timeDelta": 0.0, "artist": "", "title": "text="Spot Block End" amgTrackId="9876543"", "timestamp": "1586453290376" }]'

我想提取时间戳键的值。

我在我的 s3 存储桶中存储了一些 .meta 文件,我需要检查我的每个 .meta 文件是否有时间戳

标签: python-3.xamazon-web-servicesamazon-s3bucket

解决方案


这不是 json 格式。似乎您需要转义标题键中的 "

"title": "text=\"Spot Block End\" amgTrackId=\"9876543\""

像这样。

也许你可以尝试使用正则表达式来为你做这件事。

编辑:

import json
import re

input = b'[{ "version": "v1", "timeDelta": 0.0, "artist": "", "title": "text="Spot Block End" amgTrackId="9876543"", "timestamp": "1586453290376" }]'
input = input.decode('utf-8')

match = re.findall('(?<=\"title\": \").*\"(?=\")',input)[0]
escaped_match = json.dumps(match)
input = input.replace(match,escaped_match[1:-1])

print(json.loads(input))

看看这段代码,有点难看,但是可以。

希望能帮助到你!


推荐阅读