Changing characters in a .txt file with python, based on their frequency

I need to decrypt a simple substitutionchiffre for my cryptology homework. It is doable by hand but I think it would be quicker to code it in python.

This is the .txt:

CPTGORJLU OÜPLUCSLUSLßW ZWSUW ZJS OQ
ÜGO XQPWSW QÜA OSG ZWQPW - QRRSZ ßRQP!
SFYSPWSG ZWPSJWSG ZJLU ÜK SJG YQQP OQWSG
OJS LPSX UQW OQ GÄLU SJG YQQP APQCSG
OÄLU OSP LÄÜGWOÄXG RBÜAW

SAASßWJNJWBW ÖSZWJKKW OQZ UQGOSRG
KQG NSPRBZZW ZJLU ÖRJGO QÜA OSG QGOSP'G
DSOSP XSJM CSGQÜ, XQZ NÄG JUK QÖUBGCW
DSOSP JZW JK ZWPSZZ, OÄLU KQDÄP WÄK
KQLUW SJGSG ZLUSPV
OQGG USÖW SP QÖ ÜGO

NERRJC RÄZCSREZW
NÄG OSP SPOS
ZLUXSÖW OQZ PQÜKZLUJAA
NERRJC ZLUXSPSRÄZ


OJS SPOQGVJSUÜGCZßPQAW JZW TÖSPXÜGOSG
QRRSZ RBÜAW YSPASßW, ZLUÄG ZSJW ZWÜGOSG
XJZZSGZLUQAWRJLUS SFYSPJKSGWS
OÄLU XQZ GTWVSG OJS QK SGOS, OSGßW
ZJLU KQDÄP WÄK

JK ßÄGWPÄRRVSGWPÜK, OQ XJPO KQG YQGJZLU
OSP ßÜPZ OSP ßQYZSR, OSP ZWJKKW DQ CQP GJLUW
"UQRRÄ KQDÄP WÄK, ßEGGSG ZJS UEPSG
XÄRR'G ZJS OQZ YPÄDSßW OSGG ZÄ VSPZWEPSG?"
OÄLU SP ßQGG GJLUWZ UEP'G
SP ZLUXSÖW XSJWSP

NERRJC RÄZCSREZW
NÄG OSP SPOS
ZLUXSÖW OQZ PQÜKZLUJAA
NERRJC ZLUXSPSRÄZ

OJS SPOS ZLUJKKSPW ÖRQÜ, ZSJG RSWVWSP AÜGß ßÄKKW
"CPTMW KJP KSJGS APQÜ", ÜGO SP NSPZWÜKKW

ÜGWSG WPQÜSPG GÄLU OJS SCÄJZWSG
KQDÄP WÄK OSGßW ZJLU, XSGG OJS XTZZWSG
KJLU ATUPW UJSP SJG RJLUW OÜPLU OQZ QRR
OQZ ßSGGW JUP GÄLU GJLUW, JLU ßÄKKS ÖQRO
KJP XJPO ßQRW

NERRJC RÄZCSREZW
NÄG OSP SPOS
ZLUXSÖW OQZ PQÜKZLUJAA
ZLUXSPSRÄZ

NERRJC RÄZCSREZW
NÄG OSP SPOS
ZLUXSÖW OQZ PQÜKZLUJAA
ZLUXSPSRÄZ

NERRJC RÄZCSREZW
NÄG OSP SPOS
ZLUXSÖW OQZ PQÜKZLUJAA
NERRJC ZLUXSPSRÄZ

I already figured the frequency analysis out:

from collections import Counter
with open('/Users/StB/Downloads/text.txt', encoding='utf - 8') as f:
    c = Counter()
    for x in f:
        c += Counter(x.strip())
print (c)

Counter({' ': 168, 'S': 136, 'Z': 83, 'P': 80, 'G': 80, 'W': 76, 'J': 66, 'O': 63, 'Q': 62, 'R': 57, 'U': 57, 'L': 47, 'Ä': 43, 'K': 39, 'Ü': 28, 'X': 25, 'A': 23, 'C': 22, 'ß': 18, 'N': 17, 'E': 17, 'Ö': 14, ',': 11, 'Y': 8, 'D': 8, 'T': 6, 'V': 6, 'B': 5, '"': 4, "'": 3, 'F': 2, 'M': 2, '-': 1, '!': 1, '?': 1})

However I struggle to find a quick way to replace the characters in the .txt file, without using a loop with 26 conditions. I hope it is okay to ask for guidance!

EDIT: I forgot something that is obviously important! Substitution must be based on the frequency of the letters occuring in german words.

Character    Frequency in Words 
    E       16,11 %
    N       10,33 %
    I       9,05 %
    R       6,72 %
    T       6,34 %
    S       6,23 %
    A       5,60 %
    H       5,20 %
    D       4,17 %
    U       3,70 %
    C       3,40 %
    L       3,24 %
    G       2,94 %
    M       2,80 %
    O       2,32 %
    B       2,19 %
    F       1,71 %
    W       1,39 %
    Z       1,36 %
    K       1,33 %
    V       0,92 %
    P       0,84 %
    Ü       0,64 %
    Ä       0,51 %
    Ö       0,36 %
    ẞ       0,19 %
    J       0,19 %
    X       0,11 %
    Q       0,07 %
    Y       0,06 %
728x90

0 Answers Changing characters in a .txt file with python, based on their frequency