Добрый день! Я пишу алгоритм частотного анализа текста с помощью Visual Studio 2019 Community по этому видео:
https://www.youtube.com/watch?v=lGX2...ture=emb_title
И получил какую-то ошибку, хотя TXT, сохранённый в UTF-8, лежит в одной папке с py-файлом.
Исходный код:
Код:
line1 = "==============================================================="
line2 = "---------------------------------------------------------------"
print(line1+'''
\t\t А = 8.01% \t Б = 1.59%
\t\t В = 4.54% \t Г = 1.70%
\t\t Д = 2.98% \t Е = 8.45%
\t\t Ё = 0.04% \t Ж = 0.94%
\t\t З = 1.65% \t И = 7.35%
\t\t Й = 1.21% \t К = 3.49%
\t\t Л = 4.40% \t М = 3.21%
\t\t Н = 6.70% \t О = 10.97%
\t\t П = 2.81% \t Р = 4.73%
\t\t С = 5.47% \t Т = 6.26%
\t\t У = 2.62% \t Ф = 0.26%
\t\t Х = 0.97% \t Ц = 0.48%
\t\t Ч = 1.44% \t Ш = 0.73%
\t\t Щ = 0.36% \t Ъ = 0.04%
\t\t Ы = 1.90% \t Ь = 1.74%
\t\t Э = 0.32% \t Ю = 0.64%
\t\t Я = 2.01%
'''+line1)
name = input("File-name: ")
print(line2); text = ""
try:
with open(name,"r") as file:
original = file.read()
except FileNotFoundError:
print("File is not found!")
else:
for i in original:
if i !=" ":
text += i
else:
pass
dict = {i for i in text}
def check(words, char):
k = 0
for i in words:
if i == char: k += 1
return k
percent = 100
length = len(text)
var = 0
print("[*] Result: ")
for symbol in dict:
stat = percent * check(text,symbol) / length
if var%2 == 0:
print("\t\t{0} - {1}%\t".format(symbol,round(stat,2)),end=""); var += 1
else:
print("{0} - {1}%".format(symbol,round(stat,2))); var += 1
if var%2 == 0: print(line2)
else: print("\n"+line2)
Результат:
Код:
===============================================================
А = 8.01% Б = 1.59%
В = 4.54% Г = 1.70%
Д = 2.98% Е = 8.45%
Ё = 0.04% Ж = 0.94%
З = 1.65% И = 7.35%
Й = 1.21% К = 3.49%
Л = 4.40% М = 3.21%
Н = 6.70% О = 10.97%
П = 2.81% Р = 4.73%
С = 5.47% Т = 6.26%
У = 2.62% Ф = 0.26%
Х = 0.97% Ц = 0.48%
Ч = 1.44% Ш = 0.73%
Щ = 0.36% Ъ = 0.04%
Ы = 1.90% Ь = 1.74%
Э = 0.32% Ю = 0.64%
Я = 2.01%
===============================================================
File-name: D:\Защита информации\MyCrypt\MyCrypt\cryptOfText.TXT
---------------------------------------------------------------
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py", line 267, in run_file
runpy.run_path(options.target, run_name=compat.force_str("__main__"))
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Защита информации\MyCrypt\MyCrypt\MyCrypt.py", line 31, in <module>
except FileNotFoundError:
File "_pydevd_bundle/pydevd_cython.pyx", line 1216, in _pydevd_bundle.pydevd_cython.SafeCallWrapper.__call__
File "_pydevd_bundle/pydevd_cython.pyx", line 300, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_exception
File "_pydevd_bundle/pydevd_cython.pyx", line 187, in _pydevd_bundle.pydevd_cython.is_unhandled_exception
File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_collect_bytecode_info.py", line 167, in collect_try_except_info
def collect_try_except_info(co, use_func_first_line=False):
IndexError: list index out of range
Press any key to continue . . .
Ранее я шифровал текст алгоритмом на C# из этого видео:
https://www.youtube.com/watch?v=N_jGR7QZG-U
И хочу с помощью кода выше получить таблицу встречаемости для исходного и зашифрованного текстов.
Алгоритм шифрования:
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FinalProject
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
txtIn.Text = string.Empty;
txtOut.Text = string.Empty;
txtKey.Text = "12";
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(
"Автор: Владимир Бузин, \nДата релиза: Ноябрь 2020 г",
"Enigma v1.0",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
private void btnAction_Click(object sender, EventArgs e)
{
int key = Convert.ToInt32(txtKey.Text);
txtOut.Text = Encryption(txtIn.Text, key);
}
private string Encryption(string v1, int v2)
{
string temp = String.Empty;
foreach (char c in v1)
{
temp = temp + Convert.ToString((char)(((int)(c) ^ v2)));
}
return temp;
}
}
}