如何在python字典上隔离一个特定键。
我需要从很多视频文件中读取一些元数据。经过一些研究后,我偶然发现了。我使用了skvideo.io.ffprobe,它给了我想要的结果。它返回一个包含我要查找的信息的字典 看起来是这样的:如何在python字典上隔离一个特定键。,python,dictionary,Python,Dictionary,我需要从很多视频文件中读取一些元数据。经过一些研究后,我偶然发现了。我使用了skvideo.io.ffprobe,它给了我想要的结果。它返回一个包含我要查找的信息的字典 看起来是这样的: { "@index": "0", "@codec_name": "mjpeg", "@nb_frames": "2880", "disposition": {"@default": "1", "@dub": "0", "@timed_thumbnails": "0"}, "tag": [{"@key": "cr
{ "@index": "0", "@codec_name": "mjpeg", "@nb_frames": "2880", "disposition": {"@default": "1", "@dub": "0", "@timed_thumbnails": "0"}, "tag": [{"@key": "creation_time", "@value": "2006-11-22T23:10:06.000000Z"}, {"@key": "language", "@value": "eng"}, {"@key": "encoder", "@value": "Photo - JPEG"}]}
或者用漂亮的印刷品:
{
"@index": "0",
"@codec_name": "mjpeg",
"@nb_frames": "2880",
"disposition": {
"@default": "1",
"@dub": "0",
"@timed_thumbnails": "0"
},
"tag": [
{
"@key": "creation_time",
"@value": "2006-11-22T23:10:06.000000Z"
},
{
"@key": "language",
"@value": "eng"
},
{
"@key": "encoder",
"@value": "Photo - JPEG"
}
]
}
我的问题是如何将日期“2006-11-22T23:10:06.000000Z”分离出来。我尝试了一些不同的方法,但是我被卡住了。我找不到键或值。我想我错过了什么
我真的很感谢你的帮助
谢谢您有一个列表作为键
“tag”
的值,因此要访问它,您需要将列表从字典中取出
your_dict = #The code you're using to get that dictionary
internal_list = your_dict["tag"]
correct_dict = internal_list[0] #Because it's at the first position of the list
print(correct_dict["@value"]) #This prints the value of that dictionary from within the list at value of key "tag"
或者你可以一步到位
your_dict = #The code you're using to get that dictionary
print(your_dict["tag"][0]["@value"])
您有一个列表作为键
“tag”
的值,因此要访问它,您需要将列表从字典中取出
your_dict = #The code you're using to get that dictionary
internal_list = your_dict["tag"]
correct_dict = internal_list[0] #Because it's at the first position of the list
print(correct_dict["@value"]) #This prints the value of that dictionary from within the list at value of key "tag"
或者你可以一步到位
your_dict = #The code you're using to get that dictionary
print(your_dict["tag"][0]["@value"])
如果不假设标记列表的第一个元素包含创建时间,您可能会找到指定创建时间的位置
data = {"@index": "0", "@codec_name": "mjpeg", "@nb_frames": "2880", "disposition": {"@default": "1", "@dub": "0", "@timed_thumbnails": "0"},
"tag": [{"@key": "creation_time", "@value": "2006-11-22T23:10:06.000000Z"}, {"@key": "language", "@value": "eng"}, {"@key": "encoder", "@value": "Photo - JPEG"}]}
def get_creation_time(data):
for inner_dict in data["tag"]:
if inner_dict['@key'] == 'creation_time':
return inner_dict['@value']
raise ValueError('creation_time key value is not in tag information')
这还假设标记中的每个“内部dict”都包含@key和@value。如果不假设标记列表的第一个元素包含创建时间,您可能会发现其中指定了创建时间
data = {"@index": "0", "@codec_name": "mjpeg", "@nb_frames": "2880", "disposition": {"@default": "1", "@dub": "0", "@timed_thumbnails": "0"},
"tag": [{"@key": "creation_time", "@value": "2006-11-22T23:10:06.000000Z"}, {"@key": "language", "@value": "eng"}, {"@key": "encoder", "@value": "Photo - JPEG"}]}
def get_creation_time(data):
for inner_dict in data["tag"]:
if inner_dict['@key'] == 'creation_time':
return inner_dict['@value']
raise ValueError('creation_time key value is not in tag information')
这还假设标记中的每个“内部dict”都包含@key和@value。隔离日期是什么意思?隔离日期是什么意思?