我需要为我的密码作业解密一个简单的替代书。可以手动完成,但是我认为用python编写代码会更快。
这是.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
我已经弄清楚了频率分析:
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)
计数器({'':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,'?':1})
但是,我很难找到一种快速的方法来替换.txt文件中的字符,而无需使用具有26个条件的循环。我希望可以寻求指导!
编辑:我忘记了一些明显重要的东西!替换必须基于德语单词中字母出现的频率。
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 %