使用 RE 将条件值从键插入字典
Insert conditional values into dict from key using RE
所以我的问题很简单,我的条件是:
- if
"__"
in key, insert 'class=\"\" '
into opening tag in value pair: <tag></tag>
所以它变成: <tag class=""></tag>
然后插入 [=13 的相关值=]
- 否则
<tag></tag>
保持原样
我已经完成了其中的 95%,但还漏了一步。这是我的命令和代码:
d1 = {
"h1": "<h1 class=\"my-heading\">[]</h1>",
"h1__display-3": "<h1 class=\" \">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\" \">[]</h1>",
"h1__mt-4": "<h1 class=\"\">[]</h1>",
"h1__mt-5": "<h1 class=\" \">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\" \">[]</h1>",
"h1__my-4": "<h1 class=\" \">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2 class=\"\">[]</h2>",
"h2__mt-4": "<h2 class=\"\">[]</h2>",
"h2__my-4": "<h2 class=\"\">[]</h2>"
}
d2 = {k:k for k, v in d1.items()}
res = {}
for k in d2:
key = k.split('__')[0]
if key in d1:
res[k] = d1[key]
res2 = {k: re.sub(r'class="([\w\- ]+)"', r'class=" ' + d2[k] + '"', v) for k,v in res.items()}
res3 = {k: re.sub(r'\w\w\__', r'', v).replace('_', ' ') for k, v in res2.items()}
print('Res', json.dumps(res3, indent= 2))
在上面之后我得到不需要的:
{
"h1": "<h1 class=\"my-heading h1\">[]</h1>", # notice the unnecessary 'h1' in class
"h1__display-3": "<h1 class=\"my-heading display-3\">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\"my-heading display-3 text-white text-center\">[]</h1>",
"h1__mt-4": "<h1 class=\"my-heading mt-4\">[]</h1>",
"h1__mt-5": "<h1 class=\"my-heading mt-5\">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\"my-heading mt-5 kakabum\">[]</h1>",
"h1__my-4": "<h1 class=\"my-heading my-4\">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2>[]</h2>",
"h2__mt-4": "<h2>[]</h2>",
"h2__my-4": "<h2>[]</h2>"
}
而不是需要:
{
"h1": "<h1 class=\"my-heading\">[]</h1>",
"h1__display-3": "<h1 class=\"my-heading display-3\">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\"my-heading display-3 text-white text-center\">[]</h1>",
"h1__mt-4": "<h1 class=\"my-heading mt-4\">[]</h1>",
"h1__mt-5": "<h1 class=\"my-heading mt-5\">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\"my-heading mt-5 kakabum\">[]</h1>",
"h1__my-4": "<h1 class=\"my-heading my-4\">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2 class=\"card-title\">[]</h2>",
"h2__mt-4": "<h2 class=\"mt-4\">[]</h2>",
"h2__my-4": "<h2 class=\"my-4\">[]</h2>"
}
^注意:"h2": "<h2>[]</h2>",
的 d1 没有 class 属性
但因为键有 '__'
,class 模式已被插入 "h2__card-title": "<h2 class=\"card-title\">[]</h2>"
.
我错过了 res2 和 res3 之间的步骤,有人可以帮我吗?
好的,我通过详细阅读 python 正则表达式文档解决了这个问题,这是我的弱点:
https://docs.python.org/3.3/howto/regex.html
解决方案如下(使用与 OP 相同的字典):
d2 = {k:k for k, v in d1.items()}
mydict = {}
for k in d2:
key = k.split('__')[0]
if key in d1:
mydict[k] = d1[key]
# @params: re.sub(pattern, repl, string, max=0)
class_regex = r'class="([\w\- ]+)"'
underscore_regex = '\w+(__{1,2})'
arrow_regex = '\w+(>+)'
res2 = {k: re.sub(class_regex, r'class=" ' + (k if re.search(re.compile(underscore_regex), k) else '') + '"', v) for k,v in mydict.items()}
res3 = {k: (v if re.search(re.compile(underscore_regex), v) else (v if re.search(re.compile(class_regex), v) else re.sub(r'(>)', r' class="' + (k if re.search(re.compile(underscore_regex), k) else re.sub(class_regex, r'', '')) + '">', v, 1))) for k, v in res2.items()}
res4 = {k: re.sub(r'__', r' ', v).replace('_', ' ') for k, v in res3.items()}
res5 = {k: re.sub(r' class=\"\"', r'', v) for k, v in res4.items()}
print(json.dumps(res5, indent= 2, sort_keys=True))
这导致正确的输出:
{
"h1": "<h1 class=\"my-heading\">[]</h1>",
"h1__display-3": "<h1 class=\"my-heading display-3\">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\"my-heading display-3 text-white text-center\">[]</h1>",
"h1__mt-4": "<h1 class=\"my-heading mt-4\">[]</h1>",
"h1__mt-5": "<h1 class=\"my-heading mt-5\">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\"my-heading mt-5 kakabum\">[]</h1>",
"h1__my-4": "<h1 class=\"my-heading my-4\">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2 class=\"card-title\">[]</h2>",
"h2__mt-4": "<h2 class=\"mt-4\">[]</h2>",
"h2__my-4": "<h2 class=\"my-4\">[]</h2>"
}
所以我的问题很简单,我的条件是:
- if
"__"
in key, insert'class=\"\" '
into opening tag in value pair:<tag></tag>
所以它变成:<tag class=""></tag>
然后插入 [=13 的相关值=] - 否则
<tag></tag>
保持原样
我已经完成了其中的 95%,但还漏了一步。这是我的命令和代码:
d1 = {
"h1": "<h1 class=\"my-heading\">[]</h1>",
"h1__display-3": "<h1 class=\" \">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\" \">[]</h1>",
"h1__mt-4": "<h1 class=\"\">[]</h1>",
"h1__mt-5": "<h1 class=\" \">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\" \">[]</h1>",
"h1__my-4": "<h1 class=\" \">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2 class=\"\">[]</h2>",
"h2__mt-4": "<h2 class=\"\">[]</h2>",
"h2__my-4": "<h2 class=\"\">[]</h2>"
}
d2 = {k:k for k, v in d1.items()}
res = {}
for k in d2:
key = k.split('__')[0]
if key in d1:
res[k] = d1[key]
res2 = {k: re.sub(r'class="([\w\- ]+)"', r'class=" ' + d2[k] + '"', v) for k,v in res.items()}
res3 = {k: re.sub(r'\w\w\__', r'', v).replace('_', ' ') for k, v in res2.items()}
print('Res', json.dumps(res3, indent= 2))
在上面之后我得到不需要的:
{
"h1": "<h1 class=\"my-heading h1\">[]</h1>", # notice the unnecessary 'h1' in class
"h1__display-3": "<h1 class=\"my-heading display-3\">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\"my-heading display-3 text-white text-center\">[]</h1>",
"h1__mt-4": "<h1 class=\"my-heading mt-4\">[]</h1>",
"h1__mt-5": "<h1 class=\"my-heading mt-5\">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\"my-heading mt-5 kakabum\">[]</h1>",
"h1__my-4": "<h1 class=\"my-heading my-4\">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2>[]</h2>",
"h2__mt-4": "<h2>[]</h2>",
"h2__my-4": "<h2>[]</h2>"
}
而不是需要:
{
"h1": "<h1 class=\"my-heading\">[]</h1>",
"h1__display-3": "<h1 class=\"my-heading display-3\">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\"my-heading display-3 text-white text-center\">[]</h1>",
"h1__mt-4": "<h1 class=\"my-heading mt-4\">[]</h1>",
"h1__mt-5": "<h1 class=\"my-heading mt-5\">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\"my-heading mt-5 kakabum\">[]</h1>",
"h1__my-4": "<h1 class=\"my-heading my-4\">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2 class=\"card-title\">[]</h2>",
"h2__mt-4": "<h2 class=\"mt-4\">[]</h2>",
"h2__my-4": "<h2 class=\"my-4\">[]</h2>"
}
^注意:"h2": "<h2>[]</h2>",
的 d1 没有 class 属性
但因为键有 '__'
,class 模式已被插入 "h2__card-title": "<h2 class=\"card-title\">[]</h2>"
.
我错过了 res2 和 res3 之间的步骤,有人可以帮我吗?
好的,我通过详细阅读 python 正则表达式文档解决了这个问题,这是我的弱点: https://docs.python.org/3.3/howto/regex.html
解决方案如下(使用与 OP 相同的字典):
d2 = {k:k for k, v in d1.items()}
mydict = {}
for k in d2:
key = k.split('__')[0]
if key in d1:
mydict[k] = d1[key]
# @params: re.sub(pattern, repl, string, max=0)
class_regex = r'class="([\w\- ]+)"'
underscore_regex = '\w+(__{1,2})'
arrow_regex = '\w+(>+)'
res2 = {k: re.sub(class_regex, r'class=" ' + (k if re.search(re.compile(underscore_regex), k) else '') + '"', v) for k,v in mydict.items()}
res3 = {k: (v if re.search(re.compile(underscore_regex), v) else (v if re.search(re.compile(class_regex), v) else re.sub(r'(>)', r' class="' + (k if re.search(re.compile(underscore_regex), k) else re.sub(class_regex, r'', '')) + '">', v, 1))) for k, v in res2.items()}
res4 = {k: re.sub(r'__', r' ', v).replace('_', ' ') for k, v in res3.items()}
res5 = {k: re.sub(r' class=\"\"', r'', v) for k, v in res4.items()}
print(json.dumps(res5, indent= 2, sort_keys=True))
这导致正确的输出:
{
"h1": "<h1 class=\"my-heading\">[]</h1>",
"h1__display-3": "<h1 class=\"my-heading display-3\">[]</h1>",
"h1__display-3_text-white_text-center": "<h1 class=\"my-heading display-3 text-white text-center\">[]</h1>",
"h1__mt-4": "<h1 class=\"my-heading mt-4\">[]</h1>",
"h1__mt-5": "<h1 class=\"my-heading mt-5\">[]</h1>",
"h1__mt-5_kakabum": "<h1 class=\"my-heading mt-5 kakabum\">[]</h1>",
"h1__my-4": "<h1 class=\"my-heading my-4\">[]</h1>",
"h2": "<h2>[]</h2>",
"h2__card-title": "<h2 class=\"card-title\">[]</h2>",
"h2__mt-4": "<h2 class=\"mt-4\">[]</h2>",
"h2__my-4": "<h2 class=\"my-4\">[]</h2>"
}