繁体   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