Problem with accentuation when executing python script straight from C #

0

I'm using this to run the code:

processo.StartInfo = new System.Diagnostics.ProcessStartInfo
{
    FileName = @"\Python27\python.exe",
    Arguments = arquivoResposta,
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    RedirectStandardInput = true
};

Here is an example of the correct output:

Média mensal de atendimentos: 13.17
----
Mês 5: 17
Mês 7: 14
Mês 8: 14
Mês 11: 23
Mês 12: 30

What's coming out:

Média mensal de atendimentos: 13.17

----

Mês 5: 17

Mês 7: 14

Mês 8: 14

Mês 11: 23

Mês 12: 30

Does anyone have a solution? I tried to use output.normalize (), but still nothing.

    
asked by anonymous 24.12.2016 / 18:41

1 answer

2

What happens is that when you run a Python script straight through the command, it can detect the terminal encoding (which is "cp-852") and code the output accordingly.

When you run from another process, Python has no way of guessing the terminal encoding, and uses utf-8 by default. This is if it is Python3, or if you used Unicode strings correctly, or near the correct one in Python2 - otherwise it may be the case that you are using utf-8 hardcoded yourself.

When you attempt to display accented text, encoded in utf-8 in a latin-1 output, the accent is displayed wrong the way you posted it (each accented character unfolds in two, the first one being an "Ã")

First you have to understand what you are dealing with when it comes to encoding accents. Do not add questions, copy and paste answers, or try to keep changing parameters for "encode" and "decode" at random. This article has a good introduction .

If your program in Python2 is using byte-strings, then it is hard-coded to utf-8 itself - and if you run it directly from the command you will see the wrong accentuation, but in another way. > So, second thing - try using Python 3.6.0 or 3.5 instead of Python 2.7 - there's no sense in using an older version of Python in a new project, and the biggest change from Python2 to Python3 is just the automatic of the encoding of accents in some cases.

If the problem persists, it is because, as I said in the first paragraph, there is no way for Python to guess the encoding of its output from the data that C # sends to it in redirection. In this case, you should force the encoding to latin-1. This is unlikely to be the case, because it is quite difficult to force Python's "print" to use another encoding for the stdout other than the one that is there. What I believe to be your case, as in Python2, is that you have used byte-strings (that is, without the u"..." prefix in Python2) and etar with your hardcoded source code file in utf-8. When you start using Python3, things are resolved.

Otherwise, another way to resolve is to save your .py file with the "latin1" encoding directly. In this case, the changes should only be made in your programming text editor, and the first or second line indicating the file encoding should be changed from "# coding: utf-8" to "# coding: latin1". Your program will work without changes, but you will lose many of the tools for word processing in a correct way - so tip to switch to Python 3.

    
26.12.2016 / 15:52