Hi,
How to get result in datatable in C# after run python script?
Hi,
How to get result in datatable in C# after run python script?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 16, 2023, 12:18 PM EST
Hi Amit, thanks for posting the question. This is a great learning experience to be able to answer this question. I hope the code below might answer your question.
To get the result of a Python script in a C# DataTable, see below:
1. Create a new Python script that returns the result as a CSV file or a JSON file. For example, you can use the csv module in Python to write the result to a CSV file:
import csv # your Python code that generates the result result = [['Name', 'Age'], ['John', 30], ['Jane', 25]] # write the result to a CSV file with open('result.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) for row in result: writer.writerow(row)2. Call the Python script from C# using the Process class. Pass any necessary arguments to the Python script using the Arguments property of the ProcessStartInfo object. In this example, we assume that the Python script is named script.py and is located in the same directory as the C# code, this code calls a Python script and stores the result in a DataTable:
using System; using System.Diagnostics; using System.Data; using System.IO; class Program { static void Main(string[] args) { // create a new Process instance Process process = new Process(); // configure the ProcessStartInfo object ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "python"; // the name of the Python interpreter startInfo.Arguments = "script.py"; // the name of your Python script startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = true; process.StartInfo = startInfo; // start the process and wait for it to finish process.Start(); process.WaitForExit(); // read the output of the Python script and store it in a DataTable DataTable dataTable = new DataTable(); dataTable.Columns.Add("Name"); dataTable.Columns.Add("Age"); using (StreamReader reader = process.StandardOutput) { while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] values = line.Split(','); DataRow row = dataTable.NewRow(); row["Name"] = values[0]; row["Age"] = values[1]; dataTable.Rows.Add(row); } } // print the DataTable foreach (DataRow row in dataTable.Rows) { Console.WriteLine("{0} - {1}", row["Name"], row["Age"]); } } }In this code, we assume that the Python script returns the result as a CSV file with two columns: "Name" and "Age". The C# code reads the output of the Python script and stores it in a DataTable with the same columns. Finally, the code prints the DataTable to the console.
You can modify the code to write the DataTable to a file, display it in a UI, or perform any other operation as needed. Let me know if it helps you in a way. Good Luck.
Amit KumarRank #459 · 5.4K reputationPosted Feb 20, 2023, 3:35 AM EST
Thanks Tuhin,
But our python script return tabulate (like datatable) not in file with , seperated.
But read line read data from a file right.
Any idea for read data from datatable
Uttam KumarRank #1,076 · 1.8K reputationPosted Feb 22, 2023, 10:30 AM EST
Amit, 2nd example shown by Tuhin shows how you can handle datatable returned by python script in c#.
Uttam KumarRank #1,076 · 1.8K reputationPosted Feb 16, 2023, 11:45 PM EST
Tuhin, very well explained with the example.