2017-07-22 33 views
0

我需要從大量視頻文件中讀取一些元數據。經過一番研究,我碰到了http://www.scikit-video.org。我使用了skvideo.io.ffprobe,它給了我想要的結果。它會返回一個包含我正在查找的信息的字典。如何隔離python字典上的一個特定鍵。

它看起來像這樣:

{ "@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」 。我嘗試了一些不同的東西,但我陷入了困境。我無法獲得鍵或值。我相信我錯過了一些東西。

我真的很感激任何幫助。

感謝

+0

你是什麼意思的'''隔離日期'''? – wwii

回答

1

沒有這個標籤列表的第一個元素包含創建時間做任何假設,你可能會發現已指定 CREATION_TIME ...

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') 

這也假定標籤中的每個「內部字典」都包含@key和@value。

1

你有一個列表作爲價值的關鍵"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"]) 
相關問題