繁体   English   中英

Python 正则表达式:如果字符串元素以 $ 为前缀,则从字符串元素中删除 $ 和花括号

[英]Python REGEX: remove $ and Curly Brackets from string elements if string element is prefixed by $

在 Python 笔记本中,我有一个想要以特定方式解析的字符串,但我无法找出必要的正则表达式。 这并不重要,但该字符串之前是一个复杂的嵌套字典,该字典源自通过 json 方法将 Oozie 工作流 xml 转换为 Python 字典。

'{"workflow-app": {"@xmlns": "uri:oozie:workflow:0.4", "@name": "simple-Workflow", "start": {"@to": "Create_External_Table"}, "action": [{"@name": "Create_External_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/external.hive}"}, "ok": {"@to": "Create_orc_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Create_orc_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/orc.hive}"}, "ok": {"@to": "Insert_into_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Insert_into_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/Copydata.hive}", "param": "${database_name}"}, "ok": {"@to": "end"}, "error": {"@to": "kill_job"}}], "kill": {"@name": "kill_job", "message": "Job failed"}, "end": {"@name": "end"}}}'

无论哪种情况,您都会注意到字符串中的某些元素以美元符号为前缀。 例如“${xyz.com:8088}”、“${hdfs_path_of_script/external.hive}”等等。

其他元素也由花括号包裹,但对于那些且只有那些以美元符号为前缀的元素,我想删除美元符号前缀和立即包裹它的花括号。

在上面的两个例子中,我想获取“xyz.com:8088”和“hdfs_path_of_script/external.hive”。 这就是字符串最终的样子。

'{"workflow-app": {"@xmlns": "uri:oozie:workflow:0.4", "@name": "simple-Workflow", "start": {"@to": "Create_External_Table"}, "action": [{"@name": "Create_External_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "xyz.com:8088", "name-node": "hdfs://rootname", "script": "hdfs_path_of_script/external.hive"}, "ok": {"@to": "Create_orc_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Create_orc_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "xyz.com:8088", "name-node": "hdfs://rootname", "script": "hdfs_path_of_script/orc.hive"}, "ok": {"@to": "Insert_into_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Insert_into_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "xyz.com:8088", "name-node": "hdfs://rootname", "script": "hdfs_path_of_script/Copydata.hive", "param": "database_name"}, "ok": {"@to": "end"}, "error": {"@to": "kill_job"}}], "kill": {"@name": "kill_job", "message": "Job failed"}, "end": {"@name": "end"}}}'

有人可以帮我解析这个东西吗? 如果重要的话,我正在使用 Python 3.7。

您可以使用递归来遍历字典并更改适当的值:

import re
import json


pat = re.compile(r"\$\{(.*)\}")


def transform(d):
    if isinstance(d, dict):
        for k, v in d.items():
            if isinstance(v, str):
                d[k] = pat.sub(r"\1", v)
            else:
                transform(v)
    elif isinstance(d, list):
        for v in d:
            transform(v)


s = '{"workflow-app": {"@xmlns": "uri:oozie:workflow:0.4", "@name": "simple-Workflow", "start": {"@to": "Create_External_Table"}, "action": [{"@name": "Create_External_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/external.hive}"}, "ok": {"@to": "Create_orc_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Create_orc_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/orc.hive}"}, "ok": {"@to": "Insert_into_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Insert_into_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/Copydata.hive}", "param": "${database_name}"}, "ok": {"@to": "end"}, "error": {"@to": "kill_job"}}], "kill": {"@name": "kill_job", "message": "Job failed"}, "end": {"@name": "end"}}}'
data = json.loads(s)
transform(data)
print(json.dumps(data, indent=4))

印刷:

{
    "workflow-app": {
        "@xmlns": "uri:oozie:workflow:0.4",
        "@name": "simple-Workflow",
        "start": {
            "@to": "Create_External_Table"
        },
        "action": [
            {
                "@name": "Create_External_Table",
                "hive": {
                    "@xmlns": "uri:oozie:hive-action:0.4",
                    "job-tracker": "xyz.com:8088",
                    "name-node": "hdfs://rootname",
                    "script": "hdfs_path_of_script/external.hive"
                },
                "ok": {
                    "@to": "Create_orc_Table"
                },
                "error": {
                    "@to": "kill_job"
                }
            },
            {
                "@name": "Create_orc_Table",
                "hive": {
                    "@xmlns": "uri:oozie:hive-action:0.4",
                    "job-tracker": "xyz.com:8088",
                    "name-node": "hdfs://rootname",
                    "script": "hdfs_path_of_script/orc.hive"
                },
                "ok": {
                    "@to": "Insert_into_Table"
                },
                "error": {
                    "@to": "kill_job"
                }
            },
            {
                "@name": "Insert_into_Table",
                "hive": {
                    "@xmlns": "uri:oozie:hive-action:0.4",
                    "job-tracker": "xyz.com:8088",
                    "name-node": "hdfs://rootname",
                    "script": "hdfs_path_of_script/Copydata.hive",
                    "param": "database_name"
                },
                "ok": {
                    "@to": "end"
                },
                "error": {
                    "@to": "kill_job"
                }
            }
        ],
        "kill": {
            "@name": "kill_job",
            "message": "Job failed"
        },
        "end": {
            "@name": "end"
        }
    }
}

我可能会加载json并处理数据,但是这个正则表达式可以满足您的要求:

import re

# your original JSON
ins = '{"workflow-app": {"@xmlns": "uri:oozie:workflow:0.4", "@name": "simple-Workflow", "start": {"@to": "Create_External_Table"}, "action": [{"@name": "Create_External_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/external.hive}"}, "ok": {"@to": "Create_orc_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Create_orc_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/orc.hive}"}, "ok": {"@to": "Insert_into_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Insert_into_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "${xyz.com:8088}", "name-node": "${hdfs://rootname}", "script": "${hdfs_path_of_script/Copydata.hive}", "param": "${database_name}"}, "ok": {"@to": "end"}, "error": {"@to": "kill_job"}}], "kill": {"@name": "kill_job", "message": "Job failed"}, "end": {"@name": "end"}}}'

# this is your expected output string
outs = '{"workflow-app": {"@xmlns": "uri:oozie:workflow:0.4", "@name": "simple-Workflow", "start": {"@to": "Create_External_Table"}, "action": [{"@name": "Create_External_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "xyz.com:8088", "name-node": "hdfs://rootname", "script": "hdfs_path_of_script/external.hive"}, "ok": {"@to": "Create_orc_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Create_orc_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "xyz.com:8088", "name-node": "hdfs://rootname", "script": "hdfs_path_of_script/orc.hive"}, "ok": {"@to": "Insert_into_Table"}, "error": {"@to": "kill_job"}}, {"@name": "Insert_into_Table", "hive": {"@xmlns": "uri:oozie:hive-action:0.4", "job-tracker": "xyz.com:8088", "name-node": "hdfs://rootname", "script": "hdfs_path_of_script/Copydata.hive", "param": "database_name"}, "ok": {"@to": "end"}, "error": {"@to": "kill_job"}}], "kill": {"@name": "kill_job", "message": "Job failed"}, "end": {"@name": "end"}}}'

# replace strings that...
# * start with a "
# * then has '${'
# * capture non-greedy arbitrary number of characters with (.*?) 
# * then has '}'
# * then ends with "
# Replace it with the capture in \1 and surround with quotes
subbed = re.sub(r'"\${(.*?)}"', r'"\1"', ins)


print(subbed == outs)
# this output True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM