使用Python将值插入json文件中的特定位置 [英] Inserting values into specific locations in a json file with Python

查看:230
本文介绍了使用Python将值插入json文件中的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到一种优雅的方法来将os.walk()循环中的文件名插入到将作为JSON输出的Python对象中的 specific 子元素(需要一个更好的术语)文件.如果那没有什么意义,那么这里是我到目前为止拼凑的内容的一些视觉输出.

使用的代码:

import os
from os.path import normpath, basename
import json

ROOT_PATH = "sounds/"
jsonObject = []

for path, subdirs, files in os.walk(ROOT_PATH):
   if files:
      elementId = basename(normpath(path)) + "Audio" # <-- builds custom Id based on path
      jsonObject.append( { "elementId" : elementId, "params" : { "audioPath" : path, "sounds" : [] } } )
      for name in files:
         jsonObject.append(name)  #  <-- problem lies here...

with open('sounds/Elements.json', 'w') as outfile:
   json.dump(jsonObject, outfile, indent=3, ensure_ascii=False, sort_keys=True)

...将产生:

[
   {
      "elementId": "soundsAudio",
      "params": {
         "audioPath": "sounds/",
         "sounds": []
      }
   },
   "beep2.mp3",
   "heart_rate_flatline.mp3",
   "shhh.mp3",
   {
      "elementId": "aha_aedAudio",
      "params": {
         "audioPath": "sounds/aha_aed",
         "sounds": []
      }
   },
   "AnalyzingHeartRhythm.mp3",
   "AttachPadsToPatientsBareChest.mp3",
   "BeginCPR.mp3",
   "Charging.mp3",
   "DoNotTouchThePatient.mp3"
]

...这真的是 关闭.但是我遇到了一个麻烦,将mp3文件列表放入 sounds部分,因此看起来像这样:

[
   {
      "elementId": "soundsAudio",
      "params": {
         "audioPath": "sounds/",
         "sounds": [ "beep2.mp3",
                     "heart_rate_flatline.mp3",
                     "shhh.mp3"
         ]
      }
   },
   {
      "elementId": "aha_aedAudio",
      "params": {
         "audioPath": "sounds/aha_aed",
         "sounds": [ "AnalyzingHeartRhythm.mp3",
                     "AttachPadsToPatientsBareChest.mp3",
                     "BeginCPR.mp3",
                     "Charging.mp3",
                     "DoNotTouchThePatient.mp3"
         ]
      }
   }
]

.append.extend.insert在这一点上让我失望(或者也许我没有正确使用它们),并且进行了过于复杂的regex search-n-replace-copy-n- sounds元素的粘贴操作感觉...有点脏.

我意识到在将整个内容输出到JSON文件之前,我仍然可能会这样做.我能吸收的任何想法,技巧或解决方案示例,将不胜感激!

解决方案

没有"json对象"之类的东西-json只是一种序列化/反序列化常见数据类型(字符串,数字,数组和关联数据)的方法数组).在您的Python代码中,您所拥有的只是Python对象,在您的情况下,一个包含字典的列表(名为JsonObject)本身包含列表和字典,除了普通的基本Python数据类型,这里真的没有什么特别需要了解的.

当然,jsonObject列表中的所有列表方法都不会在这里使用,因为要存储文件列表的对象是您刚刚追加到列表的字典,而不是列表本身.

解决方案很明显:只需将文件列表添加到元素中 之前(或何时),将其添加到主列表中即可,即:

if files:
    elementId = basename(normpath(path)) + "Audio" 
    element = { 
      "elementId" : elementId, 
      "params" : { 
         "audioPath" : path, 
         "sounds" : list(files)
         },
      }
    jsonObject.append(element)

或更简单地说:

if files:
    elementId = basename(normpath(path)) + "Audio" 
    jsonObject.append({ 
      "elementId" : elementId, 
      "params" : { 
         "audioPath" : path, 
         "sounds" : list(files)
         },
      })

Trying to find an elegant way to insert filenames from a os.walk() loop into a specific sub-element (for want of a better term) in a Python object that will be output as a JSON file. If that does not make much sense, here are some visual output of what I've cobbled together so far.

code used:

import os
from os.path import normpath, basename
import json

ROOT_PATH = "sounds/"
jsonObject = []

for path, subdirs, files in os.walk(ROOT_PATH):
   if files:
      elementId = basename(normpath(path)) + "Audio" # <-- builds custom Id based on path
      jsonObject.append( { "elementId" : elementId, "params" : { "audioPath" : path, "sounds" : [] } } )
      for name in files:
         jsonObject.append(name)  #  <-- problem lies here...

with open('sounds/Elements.json', 'w') as outfile:
   json.dump(jsonObject, outfile, indent=3, ensure_ascii=False, sort_keys=True)

...which produces:

[
   {
      "elementId": "soundsAudio",
      "params": {
         "audioPath": "sounds/",
         "sounds": []
      }
   },
   "beep2.mp3",
   "heart_rate_flatline.mp3",
   "shhh.mp3",
   {
      "elementId": "aha_aedAudio",
      "params": {
         "audioPath": "sounds/aha_aed",
         "sounds": []
      }
   },
   "AnalyzingHeartRhythm.mp3",
   "AttachPadsToPatientsBareChest.mp3",
   "BeginCPR.mp3",
   "Charging.mp3",
   "DoNotTouchThePatient.mp3"
]

...and this is really close. But I've run into a brain block getting the list of mp3 files into the sounds section, so that it looks like this:

[
   {
      "elementId": "soundsAudio",
      "params": {
         "audioPath": "sounds/",
         "sounds": [ "beep2.mp3",
                     "heart_rate_flatline.mp3",
                     "shhh.mp3"
         ]
      }
   },
   {
      "elementId": "aha_aedAudio",
      "params": {
         "audioPath": "sounds/aha_aed",
         "sounds": [ "AnalyzingHeartRhythm.mp3",
                     "AttachPadsToPatientsBareChest.mp3",
                     "BeginCPR.mp3",
                     "Charging.mp3",
                     "DoNotTouchThePatient.mp3"
         ]
      }
   }
]

.append, .extend, and .insert are letting me down at this point (or maybe I'm not using them properly), and doing an overly complex regex search-n-replace-copy-n-paste operation for the sounds element feels...dirty somehow.

I realize I may be resigned to doing that anyway before outputting the whole thing into a JSON file. Any thoughts, tips, or solution examples I can absorb would be greatly appreciated!

解决方案

There's no such thing as a "json object" - json is just a way to serialize / deserialize common data types (strings, nums, arrays and associative arrays). All you have in your Python code are Python objects, in your case a list (named JsonObject) containing dicts which themselves contains lists and dicts, really nothing special to know here except plain basic Python datatypes.

Of course none of the list methods of your jsonObject list will be of any use here since the object you want to store your files list into is the dict you just appended to the list, not the list itself.

The solution is obvious: just add the files list to your element before (or when) you append it to your main list, ie:

if files:
    elementId = basename(normpath(path)) + "Audio" 
    element = { 
      "elementId" : elementId, 
      "params" : { 
         "audioPath" : path, 
         "sounds" : list(files)
         },
      }
    jsonObject.append(element)

or more simply:

if files:
    elementId = basename(normpath(path)) + "Audio" 
    jsonObject.append({ 
      "elementId" : elementId, 
      "params" : { 
         "audioPath" : path, 
         "sounds" : list(files)
         },
      })

这篇关于使用Python将值插入json文件中的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆