如何使用python检索字典中的整个数组?

时间:2016-01-12 04:08:39

标签: python json dictionary

我有一个类似的JSON文件:

{
  "bin_contents":
  {
    "bin_A":
    [
      "oreo_mega_stuf","champion_copper_plus_spark_plug","expo_dry_erase_board_eraser","kong_duck_dog_toy"
    ],
    "bin_B":
    [
      "genuine_joe_plastic_stir_sticks"
    ],
    "bin_C":
    [
      "munchkin_white_hot_duck_bath_toy"
    ],
    "bin_D":
    [
      "crayola_64_ct"
    ],
    "bin_E":
    [
      "mommys_helper_outlet_plugs","sharpie_accent_tank_style_highlighters","kong_air_dog_squeakair_tennis_ball"
    ],
    "bin_F":
    [
      "stanley_66_052"
    ],
    "bin_G":
    [
      "safety_works_safety_glasses","dr_browns_bottle_brush","laugh_out_loud_joke_book"
    ],
    "bin_H":
    [
      "cheezit_big_original","paper_mate_12_count_mirado_black_warrior"
    ],
    "bin_I":
    [
      "feline_greenies_dental_treats","elmers_washable_no_run_school_glue"
    ],
    "bin_J":
    [
      "mead_index_cards","rolodex_jumbo_pencil_cup","mead_index_cards","first_years_take_and_toss_straw_cup"
    ],
    "bin_K":
    [
      "highland_6539_self_stick_notes","mark_twain_huckleberry_finn"
    ],
    "bin_L":
    [
      "kyjen_squeakin_eggs_plush_puppies","kong_sitting_frog_dog_toy"
    ]
  },

  "work_order":
  [
    {
      "bin": "bin_A",
      "item": "oreo_mega_stuf"
    },
    {
      "bin": "bin_B",
      "item": "genuine_joe_plastic_stir_sticks"
    },
    {
      "bin": "bin_C",
      "item": "munchkin_white_hot_duck_bath_toy"
    },
    {
      "bin": "bin_D",
      "item": "crayola_64_ct"
    },
    {
      "bin": "bin_E",
      "item": "mommys_helper_outlet_plugs"
    },
    {
      "bin": "bin_F",
      "item": "stanley_66_052"
    },
    {
      "bin": "bin_G",
      "item": "safety_works_safety_glasses"
    },
    {
      "bin": "bin_H",
      "item": "cheezit_big_original"
    },
    {
      "bin": "bin_I",
      "item": "feline_greenies_dental_treats"
    },
    {
      "bin": "bin_J",
      "item": "mead_index_cards"
    },
    {
      "bin": "bin_K",
      "item": "highland_6539_self_stick_notes"
    },
    {
      "bin": "bin_L",
      "item": "kyjen_squeakin_eggs_plush_puppies"
    }
  ]
}

从以下代码段中,类对象bin_obj将其第二个参数作为data["bin_contents"]["bin_A"][1]。正如我们从JSON文件字典中看到的那样,bin_A中的bin内容有4个项目,而我只能检索一个项目。如何在类的单个对象中传递所有4个项目。

import json
from pprint import pprint

class Bin:

 def __init__(self,contents,target_item,num_items):
    print "printing function"

    self.contents = contents
    print "\ncontents are ", self.contents
    self.target_item = target_item
    print "\ntarget items is ", self.target_item
    self.num_items = num_items
    print "\nnumber of items are ", self.num_items



class Shelf(Bin):

    def __init__(self,bin_name,contents,target_item,num_items):
       Bin.__init__(self,contents,target_item,num_items)
       self.bin_name = bin_name
       print "\nBin name is", self.bin_name




def main():
    with open("apc.json") as data_file:
      data = json.load(data_file)

    inp = raw_input("enter name") 
    print "read the input", str(inp)   
    for i in range(0,12):

      if  data["work_order"][i]["bin"] == "bin_A" :  
          bin_obj = Shelf(inp,data["bin_contents"]["bin_A"][1],data["work_order"][i]["item"],1)



if __name__ == '__main__':
    main()

My current Output is:

enter name  bin    #ignore this output
read the input  bin   #ignore this output as well
printing function

contents are  champion_copper_plus_spark_plug #here it is printing just one item

target items is  oreo_mega_stuf

number of items are  1

Bin name is  b

Required Output is:

enter name b
read the input  b
printing function

contents are oreo_mega_stuf,champion_copper_plus_spark_plug,expo_dry_erase_board_eraser,kong_duck_dog_toy

target items is  oreo_mega_stuf

number of items are  1

Bin name is  b

1 个答案:

答案 0 :(得分:1)

','.join(data["bin_contents"]["bin_A"])代替data["bin_contents"]["bin_A"][1]作为contents参数,将len(data["bin_contents"]["bin_A"])作为num_items参数传递给Shelf