[英]Replacing each character with another colored character in Python
我正在處理一個單字母加密文本,我試圖根據每個字母的頻率來猜測原始文本。在逐步替換時,當我遇到下面的字母 m 時遇到錯誤是我的代碼
`cipher_text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
frequency_letters = {}
for line in cipher_text:
for char in line:
if char not in frequency_letters:
frequency_letters[char]=1
else:
frequency_letters[char] += 1
attempt = cipher_text.replace("z","\033[31ml\033[0m")
attempt = attempt.replace("i","\033[31mh\033[0m")
attempt = attempt.replace("c","\033[31mt\033[0m")
attempt = attempt.replace("y","\033[31me\033[0m")
attempt = attempt.replace("w","\033[31m''\033[0m")
attempt = attempt.replace("f","\033[31mo\033[0m")
attempt = attempt.replace("p","\033[31mw\033[0m")
attempt = attempt.replace("a","\033[31mr\033[0m")
attempt = attempt.replace("x","\033[31mi\033[0m")
attempt = attempt.replace("v","\033[31ms\033[0m")
#below is the line that gives an error substituting m to a
#attempt = attempt.replace("m","\033[31ma\033[0m")
print(attempt)
`
When it comes to substituting m to a i get these`[31[31mah[0[31ma[31[31mae[0[31ma[31[31mal[0[31ma[31[31mal[0[31ma[31[31mao[0[31ma[31[31ma''[0[31ma[31[31maw[0[31maa[31[31mal[0[31ma[31[31mat[0[31ma[31[31mae[0[31ma[31[31mar[0[31ma[31[31ma''[0[31ma[31[31mat[0[31ma[31[31mah[0[31ma[31[31mai[0[31ma[31[31mas[0[31ma[31[31ma''[0[31ma[31[31mai[0[31ma[31[31mas[0[31ma[31[31ma''[0[31ma[31[31mat[0[31ma[31[31mah[0[31ma[31[31mae[0[31ma[31[31ma''[0[31ma[31[31maw[0[31ma[31[31mao[0[31ma[31[31mar[0[31ma[31[31mas[0[31ma[31[31mat[0[31ma[31[31ma''[0[31mar[31[31mae[0[31ma[31[31mas[0[31ma[31[31mas[0[31maa [31[31mae[0[31ma[31[31ma''[0[31maq[31[31mar[0[31ma[31[31mao[0[31mar[31[31ma''`
我認為最好的解決方案是使用字典並遍歷文本
text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
d = {'z': 'l',
'i': 'h',
'c': 't',
'y': 'e',
'w': "''",
'f': 'o',
'p': 'w',
'a': 'r',
'x': 'i',
'v': 's',
'm': 'a'}
text_new = ''
for i in range(len(text)):
try:
text_new += d[text[i]].upper()
except:
text_new += text[i]
print(text_new)
我也破譯了你的文字: https : //imgur.com/sgWX9K8
也許至少使用函數為文本着色會更方便。 像這樣的東西:
cipher_text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
frequency_letters = {}
for line in cipher_text:
for char in line:
if char not in frequency_letters:
frequency_letters[char]=1
else:
frequency_letters[char] += 1
def c(character):
return "\x1b[31m" + character + "\x1b[0m"
attempt = cipher_text.replace("z",c("l"))
attempt = attempt.replace("i", c("h"))
attempt = attempt.replace("c", c("t"))
attempt = attempt.replace("y", c("e"))
attempt = attempt.replace("w", c("''"))
attempt = attempt.replace("f", c("o"))
attempt = attempt.replace("p", c("w"))
attempt = attempt.replace("a", c("r"))
attempt = attempt.replace("x", c("i"))
attempt = attempt.replace("v", c("s"))
attempt = attempt.replace("mm", c("a"))
print(attempt)
或者:
cipher_text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
frequency_letters = {}
for line in cipher_text:
for char in line:
if char not in frequency_letters:
frequency_letters[char]=1
else:
frequency_letters[char] += 1
changes = [
("z", "l"),
("i", "h"),
("c", "t"),
("y", "e"),
("w", "''"),
("f", "o"),
("p", "w"),
("a", "r"),
("x", "i"),
("v", "s"),
("mm", "a")
]
for what, to in changes:
cipher_text = cipher_text.replace(what, "\x1b[31m" + to + "\x1b[0m")
print(cipher_text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.