2016-04-21 20 views
0

キー値のペアデータを持つJSONファイルがあります。私のJSONファイルはこのように見えます。JSON出力の書式設定

{ 
    "professors": [ 
     { 
      "first_name": "Richard", 
      "last_name": "Saykally", 
      "helpfullness": "3.3", 
      "url": "http://www.ratemyprofessors.com/ShowRatings.jsp?tid=111119", 
      "reviews": [ 
       { 
        "attendance": "N/A", 
        "class": "CHEM 1A", 
        "textbook_use": "It's a must have", 
        "review_text": "Tests were incredibly difficult (averages in the 40s) and lectures were essentially useless. I attended both lectures every day and still was unable to grasp most concepts on the midterms. Scope out a good GSI to get help and ride the curve." 
       }, 
       { 
        "attendance": "N/A", 
        "class": "CHEMISTRY1A", 
        "textbook_use": "Essential to passing", 
        "review_text": "Saykally really isn't as bad as everyone made him out to be. If you go to his lectures he spends about half the time blowing things up, but if you actually read the texts before his lectures and pay attention to what he's writing/saying, you'd do okay. He posts practice tests that were representative of actual tests and curves the class nicely!" 
       }] 
     { 
     { 
     "first_name": "Laura", 
     "last_name": "Stoker", 
     "helpfullness": "4.1", 
     "url": "http://www.ratemyprofessors.com/ShowRatings.jsp?tid=536606", 
     "reviews": [ 
      { 
       "attendance": "N/A", 
       "class": "PS3", 
       "textbook_use": "You need it sometimes", 
       "review_text": "Stoker is by far the best professor. If you put in the effort, take good notes, and ask questions, you will be fine in the class. As far as her lecture, she does go a bit fast, but her lecture is in the form of an outline. As long as you take good notes, you will have everything you need for exams. She is funny and super nice if you speak with her" 
      }, 
      { 
       "attendance": "Mandatory", 
       "class": "164A", 
       "textbook_use": "Barely cracked it open", 
       "review_text": "AMAZING professor. She has a good way of keeping lectures interesting. Yes, she can be a little everywhere and really quick with her lecture, but the GSI's are useful to make sure you understand the material. Oh, and did I mention she's hilarious!" 
      }] 
    }] 

私は複数のことをやろうとしています。 私はレビューの中で最も言及された['クラス']キーを取得しようとしています。次に、クラス名とそれが言及された時間を取得します。 次に、この方法でフォーマットを出力したいと思います。また、教授の配列の下に。これは教授の情報、例えばCHEM 1A、CHEMISTRY1Aの情報です - それはRichard Saykallyです。

{ 
    courses:[ 
    { 
     "course_name" : # class name 
     "course_mentioned_times" : # The amount of times the class was mentioned 
     professors:[ #The professor array should have professor that teaches this class which is in my shown json file 
     { 
       'first_name' : 'professor name' 
       'last_name' : 'professor last name' 
     } 
    } 

私は最小値を持つところで私のjsonファイルのキー値をソートしたいと思います。これまでのところ、私はISD

if __name__ == "__main__": 
     open_json = open('result.json') 
     load_as_json = json.load(open_json)['professors'] 
     outer_arr = [] 
     outer_dict = {} 
     for items in load_as_json: 

      output_dictionary = {} 
      all_classes = items['reviews'] 
      for classes in all_classes: 
       arr_info = [] 
       output_dictionary['class'] = classes['class'] 
       output_dictionary['first_name'] = items['first_name'] 
       output_dictionary['last_name'] = items['last_name'] 
       #output_dictionary['department'] = items['department'] 
       output_dictionary['reviews'] = classes['review_text'] 
       with open('output_info.json','wb') as outfile: 
        json.dump(output_dictionary,outfile,indent=4) 
+0

可能な複製http://stackoverflow.com/questions/18871217/how-to-custom-sort-a-list-of-dict-to-use-in-json-dumps –

+1

あなたの質問タイトルはフォーマットを記述していますが、 jsonファイルのデータを注文するようなものですs。あれは正しいですか?また、あなたの入力と希望する出力が何であるかを明確にする必要があります。 – martineau

+0

Benji、Stack Overflowは質問と回答のサイトです。あなた自身のような読者は質問をし、他の読者は答えようとする。投稿には多くの情報が含まれていますが、スタックオーバーフローが発生する原因の1つが欠けています。特定のプログラミングに関する質問がありますか? –

答えて

0

を把握することができましたすべてが、私はこのプログラムが何をしたいんだと思う:問題の例の入力を使用して

import json 


with open('result.json') as open_json: 
    load_as_json = json.load(open_json) 

courses = {} 
for professor in load_as_json['professors']: 
    for review in professor['reviews']: 
     course = courses.setdefault(review['class'], {}) 
     course.setdefault('course_name', review['class']) 
     course.setdefault('course_mentioned_times', 0) 
     course['course_mentioned_times'] += 1 
     course.setdefault('professors', []) 
     prof_name = { 
      'first_name': professor['first_name'], 
      'last_name': professor['last_name'], 
     } 
     if prof_name not in course['professors']: 
      course['professors'].append(prof_name) 

courses = { 
    'courses': sorted(courses.values(), 
         key=lambda x: x['course_mentioned_times'], 
         reverse=True) 
} 
with open('output_info.json', 'w') as outfile: 
    json.dump(courses, outfile, indent=4) 

の結果、:

{ 
    "courses": [ 
     { 
      "professors": [ 
       { 
        "first_name": "Laura", 
        "last_name": "Stoker" 
       } 
      ], 
      "course_name": "PS3", 
      "course_mentioned_times": 1 
     }, 
     { 
      "professors": [ 
       { 
        "first_name": "Laura", 
        "last_name": "Stoker" 
       } 
      ], 
      "course_name": "164A", 
      "course_mentioned_times": 1 
     }, 
     { 
      "professors": [ 
       { 
        "first_name": "Richard", 
        "last_name": "Saykally" 
       } 
      ], 
      "course_name": "CHEM 1A", 
      "course_mentioned_times": 1 
     }, 
     { 
      "professors": [ 
       { 
        "first_name": "Richard", 
        "last_name": "Saykally" 
       } 
      ], 
      "course_name": "CHEMISTRY1A", 
      "course_mentioned_times": 1 
     } 
    ] 
} 
+0

私の出力は次のようになります。しかし、私は教授名が二重になっています。 "コース":[ { "教授":[ { "FIRST_NAME": "リチャード"、 "姓": "Saykally" }、 { "FIRST_NAME": "リチャード"、 「LAST_NAME ":" Saykally " }、 私は1つの教授名をriplicasではなく印刷したいと思っています。複数の教授と同じですが、名前の二重ではありません。 – Benji

+0

@ベンジ - 私は自分の答えを更新しました。 –

+0

あなたは命の恩人です。最後の1つの質問。だから私は文字と数字がどこにあるのか教科名を整形しました。私は手紙を比較したいと思うだけで、例えば、最も言及されたコースをプリントアウトしたいと思います。 CHEM 1AとCHEM 214があります - >私はインスタントCHEMとCHEMの最初の手紙を比較します - >同じです。だから私はそれらの2つの中から最も言及されたコースを私の辞書に追加するだけです – Benji