簡體   English   中英

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

[英]Python json.load returning string instead of dictionary

我正在接收一個 json 文件,並且只將必要的鍵和它們的值復制到一個新的 json 文件中。 我收到錯誤“類型錯誤:字符串索引必須是整數”,參考我將值復制到 myDict 的位置。 據我所知, json.load 返回的是字符串而不是字典。 我驗證了 json 文件,它具有有效的 json 格式。 我正在使用 Python 2.7.12。 我到處搜索,但沒有找到可以解決我的具體問題的答案。 非常感謝您能給我的任何幫助。

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 的一個例子是here。 它已大幅減少,但基本上每個相機型號都有更多條目

[
{ "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
  }]
},

在您的 json 存根中, "data"鍵包含列表。 在您的代碼中,您將其稱為字典: i.get('data').get('BaselineExposure')

相反,您應該遍歷您的"data" 例如:

data = i.get('data')
for d in data:
    print(d.get('BaselineExposure'))

所以基本上要小心嵌套項目。

還有你為什么使用i.get('id').get('value') 相反, i.get('id')應該足夠了,額外的.get('value')應該引發AttributeError ,不是嗎?

概述:我假設 json 存儲為字典。 我使用 json.dumps 將字典轉換為 json 字符串。 我 pip install json2xml json2xml 來轉換 json 字符串,以便它可以轉換為 xml。 然后我將 xml 加載到 dom 樹中進行搜索。 我使用 getElementsByTagName 在 xml 樹中搜索節點並顯示值。 我的方法對程序員更友好。

from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
from xml.dom.minidom import parse, parseString

dict={
  "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
    }]
 }

 #convert dictionary to a string
 json_data=json.dumps(dict,indent=4)
 data=readfromstring(json_data)
 xml=json2xml.Json2xml(data).to_xml()

 dom=parseString(xml)

 element=dom.getElementsByTagName('BaselineExposure') 
 print(element[0].firstChild.nodeValue)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM