2016-07-06 14 views
1

以下は私の階層データの例です。 Pythonを使用して、すべてのエントリ情報を抽出し、階層構造に基づいて整理された行と列を持つテーブルに保存する最良の方法は何ですか?各クラスタで反復処理を実行できますか?Pythonを使用した階層テキストからのデータ抽出

Root_file { 
    Version = "1.1" 
    Cluster { 
     ClusterName = "cluster 1" 
     Group { 
      groupType = Type1 
      Subgroup { 
       country = US 
      } 
     } 
     Group { 
      groupType = Type2 
      Subgroup { 
       country = England 
      } 
     } 
    } 
    Cluster { 
     ClusterName = "cluster 2" 
     Group { 
      groupType = Type1 
      Subgroup { 
       country = US 
      } 
      Subgroup { 
       country = China 
      } 
      Subgroup { 
       country = Germany 
      } 
     } 
    } 
} 
+1

これは、JSONのように見える、あなたはPythonの[JSON](https://docs.python.org/2.7/library/を使用して試してみましたjson.html) –

+2

構造は深くネストされているので、基本的にこれを2Dデータテーブルに変換する方法は、設計上の決定事項です。なぜデータをJSONエントリとして保存しないのですか? –

+0

このようなデータが私に与えられます。 JSONを提案している人には、データをJSON形式で保存してから、JSON形式のデータにコードを書き込む必要があるということですか? –

答えて

0

json形式を使用します。これをリレーショナルデータベースに保存したいのであれば、どうすればいいのか分かりません。 MongoDBを使ってjsonとしてデータを保存することができます。あなたが必要なもの

Root_file = { 
    "Version" : "1.1", 
    "Clusters" : [ 
     { 
      "ClusterName" : "cluster 1", 
      "Groups" : [ 
       { 
        "groupType" : "Type1", 
        "Subgroup" : { 
         "country" : "US" 
        } 
       }, 
       { 
        "groupType" : "Type2", 
        "Subgroup" : { 
         "country" : "England" 
        } 
       } 
      ] 
     }, 
     { 
      "ClusterName" : "cluster 2", 
      "Groups" : [ 
       { 
        "groupType" : "Type1", 
        "Subgroups" : [ 
         { 
          "country" : "US" 
         }, 
         { 
          "country" : "China" 
         }, 
         { 
          "country" : "Germany" 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 
0

は、このデータ・フォーマット用のパーサです。それは標準だ場合、私は、パーサーがあるかどうので、私は知らない、わからないが、私はのみを働くパーサを思い付いた場合は、投稿したとしてフォーマットされている場合、入力:

import re 
group_start_pattern = re.compile(r"(\w+)\s*\{") 
value_pattern = re.compile(r"(\w+)\s*\=\s*(.*)") 

def parse(src): 
    result = [] 
    current_children = result 
    current_group = None 
    for line in src.split('\n'): 
     line = line.strip() 
     if not line: 
      continue # Empty line 

     match = value_pattern.search(line) 
     if match: 
      # It's a 'key = value' line 
      if current_group is None: 
       raise SyntaxError("No current group") 
      key, value = match.groups() 
      current_group[key.strip()] = value.strip() 
      continue 

     match = group_start_pattern.search(line) 
     if match: 
      # It's a "Group {" opening line 
      current_group = { 
       'name': match.group(1), 
       'children': [], 
       'parent': current_group, 
      } 
      current_children.append(current_group) 
      current_children = current_group['children'] 
      continue 

     if line.strip() == '}': 
      # Closing group 
      if current_group is None: 
       raise SyntaxError("No current group") 
      current_group = current_group.pop('parent') 
      current_children = current_group['children'] if current_group else result 
      continue 

     raise SyntaxError("Invalid line: {!r}".format(line)) 

    if current_group: 
     raise SyntaxError("Missing closing braces") 
    return result 

与えられた文字列の場合、このパーサの出力はdictsのリストです:

[{'name': 'Root_file', 'Version': '"1.1"', 'children': [ 
    {'name': 'Cluster', 'ClusterName': '"cluster 1"', 'children': [ 
    {'groupType': 'Type1', 'children': [ 
     {'name': 'Group', 'name': 'Subgroup', 'children': [], 'country': 'US'}, 
    ]}, 
    {'name': 'Group', 'groupType': 'Type2', 'children': [ 
     {'country': 'England', 'name': 'Subgroup', 'children': []}, 
    ]}, 
    ]}, 
    {'name': 'Cluster', 'ClusterName': '"cluster 2"', 'children': [ 
    {'name': 'Group', 'groupType': 'Type1', 'children': [ 
     {'name': 'Subgroup', 'country': 'US', 'children': []}, 
     {'name': 'Subgroup', 'country': 'China', 'children': []}, 
     {'name': 'Subgroup', 'country': 'Germany', 'children': []}, 
    ]}, 
    ]}], 
}] 
関連する問題