私はjsonファイルを取り込み、必要なキーとその値を新しいjsonファイルにコピーします。私は、 "TypeError:文字列インデックスは整数でなければならない"というエラーを受け取りました。ここでmyDictに値をコピーしています。私が収集したものから、json.loadは辞書ではなく文字列を返しています。私はjsonファイルを検証し、有効なjson形式を持っています。私はPython 2.7.12を使用しています。私はどこからでも検索し、私の特定の問題に答える答えを見つけられませんでした。あなたが私に与えることができるどんな助けも大歓迎です。Python json.loadは辞書の代わりに文字列を返します
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)
jsonの例はこちらです。それを大幅に低減されているが、基本的には、各カメラモデル
[
{ "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
}]
},