【发布时间】:2019-08-21 15:40:23
【问题描述】:
re.sub 中的替换文本可以从字典中检索值吗?
本着与另一个 SO 的 post 相同的精神,使用代码
coord_re = re.sub(r"(\d), (\d)", r"\1,\2", coords)
我也想在替换字符串中使用反向引用,除了我想使用被称为 \1,\2 的字符串作为从某个字典中获取值的键
例如,我想要类似这样的东西:
d = {...somedict}
coord_re = re.sub(r"(\d), (\d)", d[value of \1]+','+d[value of \2], coords)
【问题讨论】:
标签: regex python-3.x