python - 如何按键名打印字典值?
问题描述
我知道我在这里遗漏了一些简单的东西,但需要帮助。
我有一个dict_values
看起来像这样的:
dict_values = [
[{'index': 0,
'codec_name': 'h264',
'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
'profile': 'High',
'duration': '33.400000',
'bit_rate': '310702',
'bits_per_raw_sample': '8',
'nb_frames': '835',
'disposition': {'default': 1,
'dub': 0,
'original': 0,
'comment': 0,
'lyrics': 0,
'karaoke': 0,
'forced': 0,
'hearing_impaired': 0,
'visual_impaired': 0,
'clean_effects': 0,
'attached_pic': 0,
'timed_thumbnails': 0},
'tags': {'language': 'und', 'handler_name': 'VideoHandler'}}],
{'filename': '/data/videoblocks-friends-gathering-in-campsite-around-bonfire-and-watching-movie-with-projector-on-van-side-in-dark-evening_hxdsarymm__e5cc36f7ec4f7b2cb6729b2f9a807d98__P360.mp4',
'nb_streams': 1,
'nb_programs': 0,
'format_name': 'mov,mp4,m4a,3gp,3g2,mj2',
'format_long_name': 'QuickTime / MOV',
'start_time': '0.000000',
'duration': '33.400000',
'size': '1308060',
'bit_rate': '313307',
'probe_score': 100,
'tags': {'major_brand': 'isom',
'minor_version': '512',
'compatible_brands': 'isomiso2avc1mp41',
'encoder': 'Lavf58.29.100'}},
0,
33.4,
'h264',
'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
640,
360,
'yuv420p',
'640x360 @ yuv420p',
25.0,
'25/1',
{}]
当被列入清单时,我似乎无法解决它们。似乎这里有多个字典或其他东西。我将如何通过其名称来寻址该字段?我尝试了多种方法,但在转换为列表时无法在我的 dict_value 中获得“持续时间”。
dict = *all the values above*
print(dict.get('duration'))
关于如何通过键名调用这些字段的任何想法?
解决方案
尝试这个 :
data=[[{'index': 0, 'codec_name': 'h264', 'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10', 'profile': 'High', 'duration': '33.400000', 'bit_rate': '310702', 'bits_per_raw_sample': '8', 'nb_frames': '835', 'disposition': {'default': 1, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'und', 'handler_name': 'VideoHandler'}}], {'filename': '/data/videoblocks-friends-gathering-in-campsite-around-bonfire-and-watching-movie-with-projector-on-van-side-in-dark-evening_hxdsarymm__e5cc36f7ec4f7b2cb6729b2f9a807d98__P360.mp4', 'nb_streams': 1, 'nb_programs': 0, 'format_name': 'mov,mp4,m4a,3gp,3g2,mj2', 'format_long_name': 'QuickTime / MOV', 'start_time': '0.000000', 'duration': '33.400000', 'size': '1308060', 'bit_rate': '313307', 'probe_score': 100, 'tags': {'major_brand': 'isom', 'minor_version': '512', 'compatible_brands': 'isomiso2avc1mp41', 'encoder': 'Lavf58.29.100'}}, 0, 33.4, 'h264', 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10', 640, 360, 'yuv420p', '640x360 @ yuv420p', 25.0, '25/1', {}]
print(data[0][0].get('duration'))