在我看来,您可以在 json.loads 方法的基础上构建
In [23]: from json import loads
...:
...: data = '''\
...: 1693884003 {'right': 0.6428571428571429, 'bottom': 0.9761904761904762, 'top': 0.38095238095238093, 'left': 0.22857142857142856}
...: 1693884030 {'right': 0.6571428571428571, 'bottom': 0.9285714285714286, 'top': 0.38095238095238093, 'left': 0.3142857142857143}
...: 1735837028 {'right': 0.68, 'bottom': 0.9, 'top': 0.4, 'left': 0.34}
...: 1740301012 {'right': 0.6142857142857143, 'bottom': 0.9523809523809523, 'top': 0.38095238095238093, 'left': 0.35714285714285715}
...: 1779624112 {'right': 0.7142857142857143, 'bottom': 0.9047619047619048, 'top': 0.5357142857142857, 'left': 0.21428571428571427}\
...: '''
...: images = {}
...: for line in data.splitlines():
...: image, bounds = line.split(' ', 1)
...: images[image] = loads(bounds.replace("'", '"'))
...: from pprint import pprint
...: pprint(images)
{'1693884003': {'bottom': 0.9761904761904762,
'left': 0.22857142857142856,
'right': 0.6428571428571429,
'top': 0.38095238095238093},
'1693884030': {'bottom': 0.9285714285714286,
'left': 0.3142857142857143,
'right': 0.6571428571428571,
'top': 0.38095238095238093},
'1735837028': {'bottom': 0.9, 'left': 0.34, 'right': 0.68, 'top': 0.4},
'1740301012': {'bottom': 0.9523809523809523,
'left': 0.35714285714285715,
'right': 0.6142857142857143,
'top': 0.38095238095238093},
'1779624112': {'bottom': 0.9047619047619048,
'left': 0.21428571428571427,
'right': 0.7142857142857143,
'top': 0.5357142857142857}}
In [24]:
请注意,我从字符串中读取,而您将从打开的文件中读取,
另请注意,json.loads 仅需要双引号作为分隔符,因此在使用 json.loads 之前,我们必须将数据中的单引号 replace 用双引号括起来。