如何在Python中解析此JSON文件?

问题描述 投票:-1回答:2

我有一个非常简单的任务-我有一个图像和视频文件列表,我想使用可用的EXIF数据列出每个文件的创建日期。我正在使用pyexiftool进行实际数据提取。

我可以毫无问题地提取数据,但是生成的JSON输出具有非常奇怪的形状。每条记录都有一个字段,但是该字段可以包含2或3或多位信息。

例如,某些图像文件包含XMP:CreateDateEXIF: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文件那样循环浏览。我只想提取文件名和创建日期时间。我将不胜感激任何建议。

谢谢。

python json exif exiftool
2个回答
0
投票

[通过查看您发布的代码段,它是listdict。如果格式比这更复杂,请发布更完整的示例。

这是迭代每个项目并根据找到的第一个日期字段设置日期的简单方法。

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)

0
投票

解析此“ JSON”时遇到麻烦的原因是,它是not JSON(请注意使用单引号而不是双引号)。对此[[cannot的解析没有使用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文字结构组成:字符串,字节,数字,元组,列表,字典,集合,布尔值和None
© www.soinside.com 2019 - 2024. All rights reserved.