python json load gbk,Python json.load返回字符串而不是字典

I'm taking in a json file and only copying the necesary keys and their values to a new json file. I'm getting the error "TypeError: string indices must be integers" in reference to where I'm copying the values to myDict. From what I gather, json.load is returning a string rather than a dictionary. I validated the json file and it has valid json format. I'm using Python 2.7.12. I've searched everywhere and haven't found an answer that answers my specific problem. Any help you can give me is greatly appreciated.

import os

import sys

import json

def stripSpec(inp, outp):

#Load json file as python dictionary

obj = json.load(open(inp, "r"))

result=[]

#Go through JSON and save necessary keys and values

for i in obj:

myDict = {}

myDict["id"]=i.get('id').get('value')

myDict["data"]["BaselineExposure"]=i.get('data').get('BaselineExposure').get('value')

myDict["data"]["ColorMatrix2"]=i.get('data').get('ColorMatrix2').get('value')

result.append(myDict)

# Output the updated file with pretty JSON

open(outp, "w").write(json.dumps(result, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ':')))

return

#Save input and output paths as variables

inp = sys.argv[1]

outp = sys.argv[2]

#Call function

stripSpec(inp, outp)

an example of the json is here. It has been drastically reduced but basically there is more entries for each camera model

[

{ "id": "Canon EOS 100D",

"data":[{

"SourceFile": "./Canon 100D/canon_eos_100d_11.dng",

"ExifToolVersion": 10.07,

"Directory": "./Canon 100D",

"FileSize": "18 MB",

"FileModifyDate": "2016:05:02 23:03:14-07:00",

"FileAccessDate": "2016:05:03 01:45:03-07:00",

"FileInodeChangeDate": "2016:05:02 23:03:14-07:00",

"FilePermissions": "rw-r--r--",

"ColorMatrix2": "0.6602 -0.0841 -0.0939 -0.4472 1.2458 0.2247 -0.0975 0.2039 0.6148",

"CameraCalibration1": "1.0648 0 0 0 1 0 0 0 0.9881",

"CameraCalibration2": "1.0648 0 0 0 1 0 0 0 0.9881",

"AnalogBalance": "1 1 1",

"AsShotNeutral": "0.512769 1 0.584809",

"BaselineExposure": -0.25,

"RedBalance": 1.950195

}]

},

解决方案

In your json stub "data" key contains list. In your code you refer to it as a dictionary: i.get('data').get('BaselineExposure')

Instead you should iterate through your "data".

For example:

data = i.get('data')

for d in data:

print(d.get('BaselineExposure'))

So basically be careful with nested items.

Also why do you use i.get('id').get('value'). Instead i.get('id') should be enough and additional .get('value') should raise AttributeError, isn't it?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值