[英]How do I replace all characters in a Python string?
I found a solution on stackoverflow but it doesn't seem to work.我在 stackoverflow 上找到了一个解决方案,但它似乎不起作用。 I have made a string scanner that checks for character frequency and then replaces all characters with the "real" characters.我制作了一个字符串扫描仪,用于检查字符频率,然后用“真实”字符替换所有字符。 I've made sure that the character recognition works but when I try replacing all characters in a string they no longer match up with the expected/calculated characters (when I try replacing for example only 2 characters it works fine and matches up perfectly).我已经确保字符识别有效,但是当我尝试替换字符串中的所有字符时,它们不再与预期/计算出的字符匹配(例如,当我尝试仅替换 2 个字符时,它可以正常工作并完美匹配)。 Here is my replacement code:这是我的替换代码:
print(text.replace(re,'e').replace(rt,'t').replace(ra,'a').replace(ro,'o').replace(ri,'i').replace(rn,'n').replace(rs,'s').replace(rr,'r').replace(rh,'h').replace(rl,'l').replace(ru,'u').replace(rc,'c').replace(rm,'m').replace(rf,'f').replace(ry,'y').replace(rw,'w').replace(rg,'g').replace(rp,'p').replace(rb,'b').replace(rv,'v').replace(rk,'k').replace(rx,'x').replace(rq,'q').replace(rj,'j').replace(rz,'z').replace(rd,'d'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.