2016-09-09 47 views
0

Pythonを使用してネストされたJSONをExcelファイル形式に変換したいと思います。要件ごとにほぼ完了しましたが、私は以下のようにExcel形式を達成したいと考えています。Pythonを使用してネストされたJSONをExcelに変換

JSON

[ 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Cooktops/zgbs/appliances/3741261", 
 
    "subCategory": [ 
 
     
 
    ], 
 
    "title": "Cooktops" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Dishwashers/zgbs/appliances/3741271", 
 
    "subCategory": [ 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Built-Dishwashers/zgbs/appliances/3741281", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Built-In Dishwashers" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Portable-Countertop-Dishwashers/zgbs/appliances/3741301", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Portable & Countertop Dishwashers" 
 
     } 
 
    ], 
 
    "title": "Dishwashers" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Freezers/zgbs/appliances/3741331", 
 
    "subCategory": [ 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Chest-Freezers/zgbs/appliances/3741341", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Chest Freezers" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Upright-Freezers/zgbs/appliances/3741351", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Upright Freezers" 
 
     } 
 
    ], 
 
    "title": "Freezers" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Ice-Makers/zgbs/appliances/2399939011", 
 
    "subCategory": [ 
 
     
 
    ], 
 
    "title": "Ice Makers" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Range-Hoods/zgbs/appliances/3741441", 
 
    "subCategory": [ 
 
     
 
    ], 
 
    "title": "Range Hoods" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Ranges/zgbs/appliances/3741411", 
 
    "subCategory": [ 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Drop-Ranges/zgbs/appliances/3741421", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Drop-In Ranges" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Freestanding-Ranges/zgbs/appliances/3741431", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Freestanding Ranges" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Slide-Ranges/zgbs/appliances/2399946011", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Slide-In Ranges" 
 
     } 
 
    ], 
 
    "title": "Ranges" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Refrigerators/zgbs/appliances/3741361", 
 
    "subCategory": [ 
 
     
 
    ], 
 
    "title": "Refrigerators" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Wall-Ovens/zgbs/appliances/3741481", 
 
    "subCategory": [ 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Combination-Microwave-Wall-Ovens/zgbs/appliances/3741491", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Combination Microwave & Wall Ovens" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Double-Wall-Ovens/zgbs/appliances/3741501", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Double Wall Ovens" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Single-Wall-Ovens/zgbs/appliances/3741511", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Single Wall Ovens" 
 
     } 
 
    ], 
 
    "title": "Wall Ovens" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Warming-Drawers/zgbs/appliances/2399955011", 
 
    "subCategory": [ 
 
     
 
    ], 
 
    "title": "Warming Drawers" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Washers-Dryers/zgbs/appliances/2383576011", 
 
    "subCategory": [ 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Clothes-Dryers/zgbs/appliances/13397481", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Dryers" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Clothes-Washing-Machines/zgbs/appliances/13397491", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Washers" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Combination-Washers-Dryers/zgbs/appliances/13755271", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "All-in-One Combination Washers & Dryers" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Stacked-Washer-Dryer-Units/zgbs/appliances/2399957011", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Stacked Washer & Dryer Units" 
 
     } 
 
    ], 
 
    "title": "Washers & Dryers" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Wine-Cellars/zgbs/appliances/3741521", 
 
    "subCategory": [ 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Built-Wine-Cellars/zgbs/appliances/3741551", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Built-In Wine Cellars" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Freestanding-Wine-Cellars/zgbs/appliances/3741541", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Freestanding Wine Cellars" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Furniture-Style-Wine-Cellars/zgbs/appliances/3741561", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Furniture-Style Wine Cellars" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Small-Wine-Cellars/zgbs/appliances/3741531", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Small Wine Cellars" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Wine-Cellar-Cooling-Systems/zgbs/appliances/3741581", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Wine Cellar Cooling Systems" 
 
     }, 
 
     { 
 
     "url": "https://www.amazon.com/Best-Sellers-Appliances-Wine-Rooms/zgbs/appliances/3741571", 
 
     "subCategory": [ 
 
      
 
     ], 
 
     "title": "Wine Rooms" 
 
     } 
 
    ], 
 
    "title": "Wine Cellars" 
 
    }, 
 
    { 
 
    "url": "https://www.amazon.com/Best-Sellers-Appliances-Home-Appliance-Warranties/zgbs/appliances/2242350011", 
 
    "subCategory": [ 
 
     
 
    ], 
 
    "title": "Appliance Warranties" 
 
    } 
 
]

私はこのようなすべてのサブカテゴリを横断しています:

row = 1 

def TraverseJSONTree(jsonObject, count=0): 
    title = jsonObject.get('title') 
    url = jsonObject.get('url') 

    print 'Title: ' + title + ' , Position: ' + str(count) 

    worksheet.write_string(row, count, title) 
    worksheet.write_string(row, 6, url) 
    global row 
    row+=1 

    subCategories = jsonObject.get('subCategory',[]) 

    for category in subCategories: 
     TraverseJSONTree(category, count+1) 


for jsonObject in json.loads(jsonArray): 
    TraverseJSONTree(jsonObject) 

enter image description here

期待される結果

変更

enter image description here

+0

可能なPythonコード(http://stackoverflow.com/questions/29196668/mapping-a-json-file-to-excel-with-を使用することができますPython) – bhansa

+0

どうすれば教えてくれますか?私はほぼ要件を満たすように管理してきました。私はPythonの初心者です –

答えて

0

:csvモジュールを使用することですこれを行うには 最も簡単な方法は、我々は変数全体JSONを持っていると言うあなたが希望しているだろう

import csv 
import cPickle as pickle 

fieldnames = ['Category1', 'Category1.1', 'url'] 
csvfile = open("category.csv", 'wb') 
csvfilewriter = csv.DictWriter(csvfile, fieldnames=fieldnames,dialect='excel', delimiter=',') 
csvfilewriter.writeheader() 

for b in a:  
    data = [] 
    data.append(b['title']) 
    data.append("") 
    data.append(b['url']) 
    csvfilewriter.writerow(dict(zip(fieldnames,data))) 
    data = [] 
    for i in xrange(len(b['subCategory'])): 
     data.append(b['title']) 
     data.append(b['subCategory'][i]['title']) 
     data.append(b['subCategory'][i]['url']) 
     csvfilewriter.writerow(dict(zip(fieldnames,data))) 

csvを同じ場所に置きます。これは2つのサブカテゴリのみで動作します(私はあなたが与えたデータをチェックし、2つのカテゴリ(すなわち1と1.1)しかないと言います)。しかし、同じものを繰り返す以上のものが必要な場合に備えてあなたはまた、PD pd.DataFrame.from_dict(dcitionaty_element)

として辞書に 輸入パンダを変換するために、パンダモジュールを使用し、すべての上にそれを行うことができます)、このような短い時間での任意の

を考えることができませんでしたそのjsonの辞書をマージしてcsvファイルに保存します。

+1

解決策を提供してください –

+0

これは、サブカテゴリのサブカテゴリがあれば動作するのですか? –

+0

サブカテゴリの最大数が分かっていれば、私はそれを行うことができます。forループを再度使用する必要があります。 –

1
row = 1 

def TraverseJSONTree(jsonObject, main_title=None, count=0): 
    if main_title is None: 
     main_title = title = jsonObject.get('title') 
    else: 
     title = jsonObject.get('title') 
    url = jsonObject.get('url') 

    print 'Title: ' + title + ' , Position: ' + str(count) 

    if main_title is not None: 
     worksheet.write_string(row, 0, title) 
    worksheet.write_string(row, count, title) 
    worksheet.write_string(row, 6, url) 
    global row 
    row+=1 

    subCategories = jsonObject.get('subCategory',[]) 

    for category in subCategories: 
     TraverseJSONTree(category, main_title, count+1) 

for jsonObject in json.loads(jsonArray): 
    TraverseJSONTree(jsonObject) 

それはチェックを必要として、それはカテゴリがあるならば、Excelで0番目のCOL上の元のタイトルを右に持っているあなたの予想される出力が返されますが、同じようreamin。

+0

上記のように出力が得られません。私はあなたのスクリプトをチェックしました。これは私には分かります:https://docs.google.com/spreadsheets/d/1BBlXNiCqn-Z19cCsK_kzpS4fpy4Rknek2ubyvEj4c5E/edit?usp=sharing –

+0

私はこれのようにする必要があります:https:// docs。 google.com/spreadsheets/d/1TT5alzmW9tMSR1sOFoi1YgYz_IwnTGUvhh1esHhk8QM/edit?usp=sharing となります。さらにサブカテゴリのサブカテゴリがある場合は機能します。 –

+1

ああ、愚かな間違いはちょうど次の行のmain_titleでタイトルを更新する:main_titleがない場合: workheet.write_string(行、0、main_title) –

0

あなたはそれが[パンダ]と容易になりますhere

関連する問題