Pass multiple arguments from c# to python
Question:
i want to pass multiple string variable from my program C# to Python script.
This is my function in C# where i call a file InsertSemantic.py and i want to pass there 9 arguments ( nome, formVerb, contesto ecc…)
private void insertDatabase(String nome, String formVerb, String contesto, String sinonimi, String significato, String class_gramm, String class_prag, String class_sem, String retorico)
{
string arg = string.Format(@"C:Usersmarcoeclipse-workspacemy_projectsrcInsertSemantic.py {0}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
try
{
Process p1 = new Process();
p1.StartInfo = new ProcessStartInfo(@"C:Python27python.exe", arg);
p1.StartInfo.UseShellExecute = false;
p1.StartInfo.RedirectStandardOutput = true;
p1.Start();
p1.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("There is a problem in your Python code: " + ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
I have write in InsertSemantic.py this row:
import sys
nome= sys.argv[1]
formVer=sys.argv[2]
contesto= sys.argv[3]
sinonimi=sys.argv[4]
significato=sys.argv[5]
gramm=sys.argv[6]
prag=sys.argv[7]
sem=sys.argv[8]
retorico=sys.argv[9]
but with a simple print i have seen that i receive just ONE argument (nome) and the other argument doesn’t pass…
someone can help me?
Answers:
string arg = string.Format(@"C:Usersmarcoeclipse-workspacemy_projectsrcInsertSemantic.py {0}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
is only taking the first format value with {0}
. You need to include the rest of the values as well, eg. {0},{1},{2}
et cetera.
string arg = string.Format(@"C:Usersmarcoeclipse-workspacemy_projectsrcInsertSemantic.py {0} {1} {2} {3} {4} {5} {6} {7} {8}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
the above should include the rest of the parameters to add to your formatted string.
In the future, you can solve this by stepping through your code in the debugger and you would notice that when arg
is assigned to, only the first of your parameters gets passed, indicating that the problem is at the time of assignment.
public class CallPython
{
public string Main(string[] args)
{
string arguments = args[0]; //Python script full file name including path always as the first argument
for (int i = 1; i < args.Length; i++){
arguments = arguments + " " + args[i]; //Add as many arguments as you need
};
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe";
start.Arguments = arguments;
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start)){
using (StreamReader reader = process.StandardOutput){
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
result = result.Replace("n", "").Replace("r", "");
return result;}
}
}
}
[Test]
public void TestCreateArguments()
{
CallPython program = new CallPython();
string cmd = "C:/IOIApp/Semantics.py";
string[] inputs = {cmd, "test0", "test1", "test2" };
string output_from_python=program.Main(inputs);
string cleaned = output_from_python.Replace("n", "").Replace("r", ""); //you can do some cleaning here as well
Assert.AreEqual(cleaned, "test0");
}
i want to pass multiple string variable from my program C# to Python script.
This is my function in C# where i call a file InsertSemantic.py and i want to pass there 9 arguments ( nome, formVerb, contesto ecc…)
private void insertDatabase(String nome, String formVerb, String contesto, String sinonimi, String significato, String class_gramm, String class_prag, String class_sem, String retorico)
{
string arg = string.Format(@"C:Usersmarcoeclipse-workspacemy_projectsrcInsertSemantic.py {0}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
try
{
Process p1 = new Process();
p1.StartInfo = new ProcessStartInfo(@"C:Python27python.exe", arg);
p1.StartInfo.UseShellExecute = false;
p1.StartInfo.RedirectStandardOutput = true;
p1.Start();
p1.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("There is a problem in your Python code: " + ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
I have write in InsertSemantic.py this row:
import sys
nome= sys.argv[1]
formVer=sys.argv[2]
contesto= sys.argv[3]
sinonimi=sys.argv[4]
significato=sys.argv[5]
gramm=sys.argv[6]
prag=sys.argv[7]
sem=sys.argv[8]
retorico=sys.argv[9]
but with a simple print i have seen that i receive just ONE argument (nome) and the other argument doesn’t pass…
someone can help me?
string arg = string.Format(@"C:Usersmarcoeclipse-workspacemy_projectsrcInsertSemantic.py {0}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
is only taking the first format value with {0}
. You need to include the rest of the values as well, eg. {0},{1},{2}
et cetera.
string arg = string.Format(@"C:Usersmarcoeclipse-workspacemy_projectsrcInsertSemantic.py {0} {1} {2} {3} {4} {5} {6} {7} {8}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
the above should include the rest of the parameters to add to your formatted string.
In the future, you can solve this by stepping through your code in the debugger and you would notice that when arg
is assigned to, only the first of your parameters gets passed, indicating that the problem is at the time of assignment.
public class CallPython
{
public string Main(string[] args)
{
string arguments = args[0]; //Python script full file name including path always as the first argument
for (int i = 1; i < args.Length; i++){
arguments = arguments + " " + args[i]; //Add as many arguments as you need
};
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe";
start.Arguments = arguments;
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start)){
using (StreamReader reader = process.StandardOutput){
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
result = result.Replace("n", "").Replace("r", "");
return result;}
}
}
}
[Test]
public void TestCreateArguments()
{
CallPython program = new CallPython();
string cmd = "C:/IOIApp/Semantics.py";
string[] inputs = {cmd, "test0", "test1", "test2" };
string output_from_python=program.Main(inputs);
string cleaned = output_from_python.Replace("n", "").Replace("r", ""); //you can do some cleaning here as well
Assert.AreEqual(cleaned, "test0");
}