3

I want to pass Arabic string to python script from c#. And then I will write output to txt file. But my arabic string parameter couldn't pass correctly. It transforms to "????" question marks.

I tried some code, like unicode, but it doesn't work

if __name__ == '__main__':
    my_input = sys.argv[1]
    out = open("C:\\output.txt", "wb")
    p = unicode(my_input, encoding='utf-8')
    out.write(p)
    out.close()

Also my c# code is like that

public static string run_cmd(string args)
{
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = "C:/Python27/python.exe";
    start.Arguments = "C:/test.py" + " " + args;
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    using (Process process = Process.Start(start))
    {
        using (StreamReader reader = process.StandardOutput)
        {
           result = reader.ReadToEnd();
        }
    }
}

Do you have any idea how can I fix this problem? Or is there something that I missing?

Any idea will be great!

jakobiyem
  • 107
  • 1
  • 5
  • 15
  • when you do `print(p)` does it print correctly? And I am guessing this is Python 2.x ? – Anand S Kumar Aug 20 '15 at 06:48
  • No, it prints ??? again. And i should say that, question mark number is equal to arabic string character number. And yes, i am using Python 2.7 @AnandSKumar – jakobiyem Aug 20 '15 at 06:51
  • UTF-8 is probably the problem. Does Python have UTF-16 or UTF-32? – WDS Aug 20 '15 at 06:51
  • I agree, utf-8 is almost certainly the problem. Arabic characters aren't included in it, but I think python does support utf-16/32. You should just try and change the encoding argument to "utf-16". – Rational Function Oct 19 '15 at 16:51

0 Answers0