簡體   English   中英

使用Python從層次文本中提取數據

[英]Data extraction from hierachial text using 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
            }
        }
    }
}

使用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"
                        }
                    ]
                }
            ]
        }
    ]
}

您需要的是此數據格式的解析器 我不知道它是否是一個標准,因此我不知道是否有解析器,但我想出了一個解析器, 只有在輸入格式化時才會起作用:

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': []},
    ]},
  ]}],
}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM