【问题标题】:Display a python output with special characters in GUI label unity在 GUI 标签统一中显示带有特殊字符的 python 输出
【发布时间】:2018-06-16 00:20:15
【问题描述】:

我正在统一运行 python 脚本,我希望输出统一显示在 GUI 标签中,输出包含特殊字符,如 é、ç、...,但我正在获取符号。 这是我迄今为止尝试过的,但没有运气

using (Process proc = Process.Start (start)) {
            using (StreamReader reader = proc.StandardOutput) {
                string result = reader.ReadToEnd ();
                UTF8Encoding utf8 = new UTF8Encoding ();
                byte[] encodedBytes = utf8.GetBytes (result);
                UnityEngine.Debug.Log (result);
                Message1.text = result; 
            }
        }

result 是输出:应该是

('fromaje', 'Fausse:')
('Correction suggérér:','fromage')

但我得到了:

【问题讨论】:

  • 我认为我们应该首先尝试找出问题发生在哪里。您能否尝试记录一个 utf8 字符串并查看它是否会正确打印?你确定 python 脚本正在打印有效的 utf8 输出吗?
  • 您使用的是哪个统一版本?它确实支持这种字符。
  • 这听起来像是一个基本的转换问题:byte[] bytes = Encoding.Default.GetBytes(myString); myString = Encoding.UTF8.GetString(bytes); //显示我的字符串

标签: c# python-3.x unity3d utf-8 special-characters


【解决方案1】:

创建流程时使用:

    var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "python.exe whatever.py",
        Arguments = "yourargs",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
 };

然后通过这样做读取字符串:

proc.Start();
StringBuilder bldr = new StringBuilder();
while (!proc.StandardOutput.EndOfStream) {
    bldr.append(proc.StandardOutput.ReadLine()+"\n");
}

您可以通过以下方式获得完整的输出:

bldr.ToString()

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多