0

执行时我无法获得正确的输出值

print metadata["File:FileSize"]

它给了我list indices must be a integer, not str错误。我想json.loads返回一本字典。

metadata像这样从类中的 exiftool 检索变量:

def get_metadata(self, *filenames):
    return json.loads(self.execute("-G", "-j", "-n", *filenames))

在主程序流程中我这样做:

metadata = exif.get_metadata(fileName)
print metadata
print metadata["File:FileSize"]

有人可以帮我弄清楚我做错了什么吗?

这是原始 exiftool 查询的示例:

[{
  "SourceFile": "/media/mango/MF-HDD-277/01_audio/Computer Arts royalty-free audio samples/Disc 191 - soundsnap/ComputerArtsDisc191_SoundSnapSamples_088_Bend03.wav",
  "ExifTool:ExifToolVersion": 9.13,
  "File:FileName": "ComputerArtsDisc191_SoundSnapSamples_088_Bend03.wav",
  "File:Directory": "/media/mango/MF-HDD-277/01_audio/Computer Arts royalty-free audio samples/Disc 191 - soundsnap",
  "File:FileSize": 146948,
  "File:FileModifyDate": "2010:10:24 11:17:20-04:00",
  "File:FileAccessDate": "2014:01:29 15:58:48-05:00",
  "File:FileInodeChangeDate": "2014:01:21 13:28:00-05:00",
  "File:FilePermissions": 711,
  "File:FileType": "WAV",
  "File:MIMEType": "audio/x-wav",
  "RIFF:Encoding": 1,
  "RIFF:NumChannels": 2,
  "RIFF:SampleRate": 44100,
  "RIFF:AvgBytesPerSec": 176400,
  "RIFF:BitsPerSample": 16,
  "Composite:Duration": 0.833038548752834
}]
4

1 回答 1

2

原始查询返回数组内的 javascript 对象。当你用 解析 JSON 时json.loads,你会得到一个列表中的字典。因此,要访问属性,请执行metadata[0]["File:FileSize"].

或者改变get_metadata方法直接返回dict。

def get_metadata(self, *filenames):
    return json.loads(self.execute("-G", "-j", "-n", *filenames))[0]

不,json.loads不仅返回字典。Tim 在他的评论中链接的表指定了 JSON 到 Python 的翻译。

于 2014-03-11T14:19:30.913 回答