简体   繁体   中英

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

`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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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