[英]How to parse this JSON file in Python?
我有一个非常简单的任务 - 我有一个图像和视频文件列表,我想使用可用的 EXIF 数据将每个文件的创建日期制成表格。 我正在使用pyexiftool进行实际的数据提取。
我可以毫无问题地提取数据,但是生成的 JSON output 的形状非常奇怪。 每条记录都有一个字段,但该字段可能包含 2 或 3 或多个信息位。
例如,一些图像文件包含XMP:CreateDate
和EXIF:CreateDate
,而 MOV 文件包含“QuickTime:CreateDate”(我不知道其他文件格式的字段是什么)。
[{'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200422_085514.JPG', 'EXIF:CreateDate': '2020:04:22 08:55:14', 'XMP:CreateDate': '2020:04:22 08:55:14'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200423_091856.JPG', 'EXIF:CreateDate': '2020:04:23 09:18:57'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200423_091859.JPG', 'EXIF:CreateDate': '2020:04:23 09:19:00', 'XMP:CreateDate': '2020:04:23 09:19:00'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0004.mp4', 'QuickTime:CreateDate': '2017:03:11 13:05:59'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0005.mp4', 'QuickTime:CreateDate': '2017:03:11 13:08:26'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0006.mp4', 'QuickTime:CreateDate': '2017:03:11 13:09:17'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0035.mp4', 'QuickTime:CreateDate': '2017:03:12 14:08:55'}]
我对如何解析这个文件很迷茫,我不能像普通的 JSON 文件那样循环遍历它。 我只想提取文件名和创建日期时间。 我会很感激任何建议。
谢谢。
编辑产生'JSON' output 的代码是这样的,
def old_main():
dir_name = '/Users/Documents/Projects/ExifData/temp/'
tags = ["File Name", "CreateDate"]
log_file = 'py_log.txt'
file_names = getListOfFiles(dir_name)
with exiftool.ExifTool() as e:
metadata = e.get_tags_batch(tags, file_names)
with open(log_file, "w") as outfile:
json.dump(metadata, outfile)
所以我贴的是json.dump
方法的直接output。 此处记录了get_tags_batch
方法。
除非我误解了 package 的文档,否则看起来 output 根本不是 JSON 而是一个字符串?
欣赏指针和评论。
通过查看您发布的代码段,它是一个dict
list
。 如果格式比这更复杂,请发布更完整的示例。
这是一种迭代每个项目并根据找到的第一个日期字段设置日期的简单方法。
results = []
for item in json_list:
d = {'SourceFile': item['SourceFile']}
date_keys = [k for k in item.keys() if 'CreateDate' in k]
if date_keys:
d['Date'] = item[date_keys[0]]
else:
d['Date'] = None
results.append(d)
您无法解析此“JSON”的原因是它不是JSON
(注意使用单引号而不是双引号)。 这不能用JSON
解析器未经修改地解析。
而是使用:
from ast import literal_eval
t = """[{'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200422_085514.JPG', 'EXIF:CreateDate': '2020:04:22 08:55:14', 'XMP:CreateDate': '2020:04:22 08:55:14'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200423_091856.JPG', 'EXIF:CreateDate': '2020:04:23 09:18:57'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200423_091859.JPG', 'EXIF:CreateDate': '2020:04:23 09:19:00', 'XMP:CreateDate': '2020:04:23 09:19:00'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0004.mp4', 'QuickTime:CreateDate': '2017:03:11 13:05:59'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0005.mp4', 'QuickTime:CreateDate': '2017:03:11 13:08:26'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0006.mp4', 'QuickTime:CreateDate': '2017:03:11 13:09:17'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0035.mp4', 'QuickTime:CreateDate': '2017:03:12 14:08:55'}]"""
o = literal_eval(t)
print(o)
印刷:
[{'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200422_085514.JPG', 'EXIF:CreateDate': '2020:04:22 08:55:14', 'XMP:CreateDate': '2020:04:22 08:55:14'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200423_091856.JPG', 'EXIF:CreateDate': '2020:04:23 09:18:57'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/IMG_20200423_091859.JPG', 'EXIF:CreateDate': '2020:04:23 09:19:00', 'XMP:CreateDate': '2020:04:23 09:19:00'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0004.mp4', 'QuickTime:CreateDate': '2017:03:11 13:05:59'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0005.mp4', 'QuickTime:CreateDate': '2017:03:11 13:08:26'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0006.mp4', 'QuickTime:CreateDate': '2017:03:11 13:09:17'}, {'SourceFile': '/Users/Documents/Projects/ExifData/temp/MOV_0035.mp4', 'QuickTime:CreateDate': '2017:03:12 14:08:55'}]
根据手册, literal_eval
:
安全地评估包含 Python 文字或容器显示的表达式节点或字符串。 提供的字符串或节点只能由以下 Python 文字结构组成:字符串、字节、数字、元组、列表、字典、集合、布尔值和无
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.