我有一个问题需要解决,例如:
s1 = "1$<2d>46<2e>5"
str_dict = {
"<2c>": ",",
"<2e>": ".",
"<2f>": "/",
"<2d>": "-",
"<2b>": "+",
"<3d>": "=",
"<3f>": "?",
"<2a>": "*",
"<5d>": "]",
"<40>": "@",
"<23>": "#",
"<25>": "%",
"<5e>": "^",
"<26>": "&",
"<5f>": "_",
"<5c>": "\\",
"<24>": "$",
}
def str_replace(source):
target = source
for k, v in str_dict.items():
if k in source:
target = source.replace(k, v)
str_replace(target)
return target
我想将s1
中的这些特殊str
替换为它们的dict'
值
str_replace(source)
是我的职责
但是如果特殊的str
在s1
中不止一个,那么它只会替换第一个位置
我只是对你的
str_replace()
做了一点改动,似乎效果不错:如果没有设置
count
,则str.replace(old, new[, count])
可以替换所有匹配的模式相关问题 更多 >
编程相关推荐