1

I want to run a Python script from C#. This has been working fine, following the approach shown here

I am retrieving the values using argv inside the python script The issue arises, when I try to pass float values to my python application. I'm not sure whether argv can't separate the floating point number properly or why they don't seem to arrive.

My Code: C#:

using System;
using System.IO;
using System.Diagnostics;

namespace CallPython
{

class ProgramIntercom
{
    static void Main(string[] args)
    {
        // full path of python interpreter 
        string python = @"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe";

        // python app to call 
        string myPythonApp = "TestIntercom.py";

        // parameters to send to the Python script 
        float num1 = 1;
        float num2 = 1.6125f;
        float num3 = 2;
        float num4 = 0.2f;
        float num5 = -2;
        float num6 = 0.9046666667f;

        // Create new process start info 
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

        // make sure we can read the output from stdout 
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcessStartInfo.CreateNoWindow = true;

        // start python app with 7 arguments  
        // 1st arguments is pointer to itself,  
        // the next 6 are actual arguments we want to send (Python needs to filter out the first one)
        myProcessStartInfo.Arguments = myPythonApp + " " + num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5 + " " + num6;
        Process myProcess = new Process();
        // assign start information to the process 
        myProcess.StartInfo = myProcessStartInfo;

        Console.WriteLine("Calling Python script with arguments: {0}, {1}, {2}, {3}, {4}, {5} ", num1, num2, num3, num4, num5, num6);
        // start the process 
        myProcess.Start();

        // Read the standard output of the app we called.  
        // in order to avoid deadlock we will read output first 
        // and then wait for process terminate: 
        StreamReader myStreamReader = myProcess.StandardOutput;
        string result = myStreamReader.ReadToEnd();

        /*if you need to read multiple lines, you might use: 
            string myString = myStreamReader.ReadToEnd() */

        // wait exit signal from the app we called and then close it. 
        myProcess.WaitForExit();
        myProcess.Close();

        // write the output we got from python app 
        Console.WriteLine("Value received from script: " + result);

    }
}
}

The Example Python Script:

from sys import argv

# Get the arguments from the command line
python_path, num1, num2 , num3, num4, num5, num6 = argv

# sum up the two numbers
sum = float(num1) + float(num2) + float(num3) + float(num4) + float(num5) + float(num6)

# print the sum
print(sum)

When using floats instead of integers no result is printed out in the C# application

FLOROID
  • 13
  • 6
  • 2
    Hint: add some kind of logging in your Python Script to analyze incoming arguments. – RebootDeluxe Nov 08 '22 at 16:30
  • @RebootDeluxe the issue seems to be that the floating point numbers arrive comma-separated, not point-separated. So 1.6125 -> 1,6125 – FLOROID Nov 08 '22 at 16:39
  • Then this may help you: https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python – RebootDeluxe Nov 08 '22 at 16:46

1 Answers1

0

The reason why this happens appears to be due to an automatic conversion of the floating point number in US format to the German format (My system language is German). This is a common issue in european countries.

In order to circumvent this I have used the following operation on these values:

using System.Globalization;
string num1 = a_float.ToString(CultureInfo.CreateSpecificCulture("en-US"));

This will ensure, that the floating point numbers are written with a dot instead of a comma.

I assume that if you're using an American or British system this won't be an issue in the first place.

FLOROID
  • 13
  • 6