В какой кодировке модуль Python3 выводит текст?
Запускаю процесс "imageToText.py", который печатает результат: print("какой то русский текст")
а в консоли приложения получается текст в какой-то кодировке
internal static string[] GetImagesCaptions(IEnumerable<string> imagesPaths)
{
string path = @"C:\Users\Alexey\source\repos\PythonApplication2\GlobalEnv\imageToText.py";
string pythonPath = @"C:\Users\Alexey\AppData\Local\Programs\Python\Python310\python.exe";
string imagesRow = imagesPaths.Select(p => $" \"{p}\"").JoinToString();
ProcessStartInfo python = new()
{
FileName = pythonPath,
Arguments = "-W ignore " + path + imagesRow,
UseShellExecute = false,
RedirectStandardOutput = true,
//StandardOutputEncoding = ?
};
using Process? process = Process.Start(python);
ArgumentNullException.ThrowIfNull(process);
using StreamReader reader = process.StandardOutput;
return reader.ReadToEnd().Split(Environment.NewLine);
/**
"╟ръЁ√Єшх ЄрсышЎ√ ё ўшёыюь ы■фхщ ш ъюышўхёЄтюь ы■фхщ"
"└ЁрЇшЁютрээр ърЁЄр ьшЁр ёю ёЄЁрэрьш т ёшэхь ш ёшэхь ЎтхЄх"
""
*/
}
Это не в какой-то кодировке. Из-за того, что вы явно не указали, что за кодировка используется в читаемом файле, произошла неправильная интерпретация байтов.
Вот как можно выводить корректно данные для кодировки UTF-8:
internal static string[] GetImagesCaptions(IEnumerable<string> imagesPaths)
{
string path = @"C:\Users\Alexey\source\repos\PythonApplication2\GlobalEnv\imageToText.py";
string pythonPath = @"C:\Users\Alexey\AppData\Local\Programs\Python\Python310\python.exe";
string imagesRow = imagesPaths.Select(p => $" \"{p}\"").JoinToString();
ProcessStartInfo python = new()
{
FileName = pythonPath,
Arguments = "-W ignore " + path + imagesRow,
UseShellExecute = false,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8 // Установка кодировки UTF-8
};
using Process? process = Process.Start(python);
ArgumentNullException.ThrowIfNull(process);
using StreamReader reader = process.StandardOutput;
return reader.ReadToEnd().Split(Environment.NewLine);
}