简体   繁体   English

用Python中的另一个彩色字符替换每个字符

[英]Replacing each character with another colored character in Python

I am working on a monoalphabetic encrypted text and i am trying to guess the original text based on the frequency of each letter .While substituting progressively i encountered an error when it came to the letter m below is my code我正在处理一个单字母加密文本,我试图根据每个字母的频率来猜测原始文本。在逐步替换时,当我遇到下面的字母 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''`

I think the best solution is to use a dictionary and to iterate through the text我认为最好的解决方案是使用字典并遍历文本

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)

Also I deciphered your text: https://imgur.com/sgWX9K8我也破译了你的文字: https : //imgur.com/sgWX9K8

And perhaps it would be more convenient to use a function to colorize text, at least.也许至少使用函数为文本着色会更方便。 Something like this:像这样的东西:

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)

Or:或者:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM